Indigo Beta1 -> WCF Beta2
Omri Gazitt's 的Weblog 指出了Indigo Beta1 -> WCF Beta2 的一些改动和变化,
https://www.gazitt.com/Blog/PermaLink,guid,1ff8c791-2c1c-4192-9614-3cf79e90867d.aspx
摘要如下:
对象模型/API方面
ServiceHost<T> -> ServiceHost
ServiceSite -> InstanceContext
InstanceModeAttribute -> InstanceContextModeAttribute
InstanceModeAttribute.Singleton -> InstanceContextModeAttribute.Single
typeof(ChannelFactory.Description.Address) -> EndpointAddress (from Uri)
IProxyChannel -> IClientChannel
ProxyBase -> ClientBase
配置方面
@bindingSectionName on <endpoint> -> @binding
@contractType on <endpoint> -> @contract
<netProfileTcpBinding> -> <netTcpBinding>
<wsProfileBinding> -> <wsHttpBinding>
<basicProfileBinding> -> <basicHttpBinding>
@serviceType on <service> -> @type
Also, all the customPeerResolver stuff goes away, becomes netPeerTcpBinding.
试了一下David Pallmann的Programming "Indigo"第二章最简单的Hello World 1例子,在WCF Beta2 上果然不能编译,按照Omri Gazitt的Blog进行修改
服务器端:
ServiceHost<HelloService> serviceHost = new ServiceHost<HelloService>()->
ServiceHost serviceHost = new ServiceHost(typeof(HelloService), uri)
AddEndpoint-> AddServiceEndpoint
WSProfileBinding -> WSHttpBinding
客户端的变化
IHello proxy = ChannelFactory.CreateChannel<IHello>(uri, binding); ->
EndpointAddress endpoint = new EndpointAddress(uri);
ChannelFactory<IHello> factory = new ChannelFactory<IHello>(binding, endpoint);
IHello proxy = factory.CreateChannel();
完整的代码
services sides
using System;
using System.ServiceModel;
namespace ProgrammingIndigo
{
//Contract definition.
[ServiceContract]
public interface IHello
{
[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);
}
// Service implementation.
public class HelloService : IHello
{
public double Add(double n1, double n2)
{
Console.WriteLine("Add called");
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
Console.WriteLine("Subtract called");
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
Console.WriteLine("Multiply called");
return n1 * n2;
}
public double Divide(double n1, double n2)
{
Console.WriteLine("Divide called");
return n1 / n2;
}
// Host the service.
public static void Main()
{
// Create a ServiceHost.
Uri uri = new Uri("https://localhost:8000/hello1/");
using (ServiceHost serviceHost = new ServiceHost(typeof(HelloService), uri))
{
// Add an endpoint.
WSHttpBinding binding = new WSHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IHello), binding, uri);
// Open the service.
serviceHost.Open();
// The service can now be accessed. Hold it open until user presses ENTER.
Console.WriteLine("The service is ready");
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down service.");
Console.WriteLine();
Console.ReadLine();
// Close the service.
serviceHost.Close();
}
}
}
}
client sides
using System;
using System.ServiceModel;
namespace ProgrammingIndigo
{
//Contract definition.
[ServiceContract]
public interface IHello
{
[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);
}
//Client implementation code.
class Client
{
static void Main()
{
// Create a proxy.
WSHttpBinding binding = new WSHttpBinding();
Uri uri = new Uri("https://localhost:8000/hello1/");
EndpointAddress endpoint = new EndpointAddress(uri);
ChannelFactory<IHello> factory = new ChannelFactory<IHello>(binding, endpoint);
IHello proxy = factory.CreateChannel();
//IHello proxy = ChannelFactory.CreateChannel<IHello>(uri, binding);
try
{
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
Console.WriteLine("Calling Add({0},{1})", value1, value2);
double result = proxy.Add(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
Console.WriteLine("Calling Subtract({0},{1})", value1, value2);
result = proxy.Subtract(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
Console.WriteLine("Calling Multiply({0},{1})", value1, value2);
result = proxy.Multiply(value1, value2);
Console.WriteLine(" Result: {0}", result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
Console.WriteLine("Calling Divide({0},{1})", value1, value2);
result = proxy.Divide(value1, value2);
Console.WriteLine(" Result: {0}", result);
}
finally
{
((IChannel)proxy).Close();
((IChannel)proxy).Dispose();
}
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down client");
Console.ReadLine();
}
}
}