动态生成 WCF Client Proxy 代码 —— My Svcutil
在《.NET Framework 3.0 之旅》中我们使用 ChannelFactory 来创建服务对象,或许你会觉得此方式有悖于 SOA 原则。没问题,现在我们使用另外一种方式来达到和 svcutil 同样的效果。 目标服务原型
[ServiceContract]
public interface ICalculate
{
[OperationContract]
double Add(double a, double b);
}
public class CalculateService : ICalculate
{
public double Add(double a, double b)
{
return a + b;
}
}
public interface ICalculate
{
[OperationContract]
double Add(double a, double b);
}
public class CalculateService : ICalculate
{
public double Add(double a, double b)
{
return a + b;
}
}
服务器代码
ServiceHost host = new ServiceHost(typeof(CalculateService));
host.AddServiceEndpoint(typeof(ICalculate), new BasicHttpBinding(), "http://localhost:8080/calc");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:8080/calc");
host.Description.Behaviors.Add(behavior);
host.Open();
host.AddServiceEndpoint(typeof(ICalculate), new BasicHttpBinding(), "http://localhost:8080/calc");
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost:8080/calc");
host.Description.Behaviors.Add(behavior);
host.Open();
我们创建了一个 ServiceMetadataBehavior,只有这样我们才能获得 WSDL / MetaData。
动态创建客户端端代理代码
动态创建客户端端代理代码
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
Uri uri = new Uri("http://localhost:8080/calc");
MetadataExchangeClient client = new MetadataExchangeClient(uri, MetadataExchangeClientMode.HttpGet);
MetadataSet metadata = client.GetMetadata();
WsdlImporter importer = new WsdlImporter(metadata);
CodeCompileUnit ccu = new CodeCompileUnit();
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
ServiceContractGenerator generator = new ServiceContractGenerator(ccu);
foreach (ContractDescription description in importer.ImportAllContracts())
{
generator.GenerateServiceContractType(description);
}
StringWriter writer = new StringWriter();
provider.GenerateCodeFromCompileUnit(ccu, writer, null);
string code = writer.ToString();
File.WriteAllText("proxy.cs", code, Encoding.Default);
using System.Collections.Generic;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
Uri uri = new Uri("http://localhost:8080/calc");
MetadataExchangeClient client = new MetadataExchangeClient(uri, MetadataExchangeClientMode.HttpGet);
MetadataSet metadata = client.GetMetadata();
WsdlImporter importer = new WsdlImporter(metadata);
CodeCompileUnit ccu = new CodeCompileUnit();
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
ServiceContractGenerator generator = new ServiceContractGenerator(ccu);
foreach (ContractDescription description in importer.ImportAllContracts())
{
generator.GenerateServiceContractType(description);
}
StringWriter writer = new StringWriter();
provider.GenerateCodeFromCompileUnit(ccu, writer, null);
string code = writer.ToString();
File.WriteAllText("proxy.cs", code, Encoding.Default);
运行上述代码后,打开 proxy.cs,你看到了什么?好了,把这个文件加到客户端项目中去就 OK 了。其实利用此方式,我们还可以动态生成客户端代理程序集,实现动态调用,有关此方式,可以参考我以前写的《动态调用 WebService》。