Open makaveli713 opened 11 years ago
Address, binding, contract
_файл->создать проект->WCF->библиотека службы WCF (GettingStarted)_
using System.ServiceModel;
namespace GettingStartedLib
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples/")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}
using System;
namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}
}
_создать консольное приложение(GettingStartedHost) и добавить ссылки на созданную службу и на System.ServiceModel_
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using GettingStartedLib;
namespace GettingStartedHost
{
class Program
{
static void Main()
{
// step 1. Create a URI to serve as the base address.
var baseAddress = new Uri("http://localhost:8000/GettingStarted");
// step 2. Create a server host instance.
var selfHost = new ServiceHost(typeof (CalculatorService), baseAddress);
try
{
// step 3. Add a service endpoint.
selfHost.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "CalculatorService");
// step 4. Enable metadata exchange.
var smb = new ServiceMetadataBehavior
{
HttpGetEnabled = true
};
selfHost.Description.Behaviors.Add(smb);
// step 5. Start the service.
selfHost.Open();
Console.WriteLine("The service is ready");
Console.WriteLine("Press <ENTER> to terminate service");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException exception)
{
Console.WriteLine(exception.Message);
selfHost.Abort();
}
}
}
}
_Сначала нужно добавить ссылку на службу_ http://localhost:8000/GettingStarted
и на System.ServiceModel
using System;
using GettingStartedClient.ServiceReference1;
namespace GettingStartedClient
{
class Program
{
static void Main()
{
Console.ReadLine();
}
}
}
edit App.config
_Открыть Program.cs в GettingStartedClient_
using System;
using GettingStartedClient.ServiceReference1;
namespace GettingStartedClient
{
class Program
{
static void Main()
{
// step 1. Create an instance of the WCF proxy.
var client = new CalculatorClient();
// step 2. Call the servic operation.
// Call the Add service operation.
var value1 = 100.00D;
var value2 = 15.99D;
var result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.ReadLine();
}
}
}
A. Troelsen page #906 (chapter 25)