避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)

为了简化WCF的开发,我们可以通过在项目中Add Service Reference的方式(或svcutil)生成一个代理类。
后来P&P提供了Service Factory,该工程可以帮助我们设计WCF, 并根据需要灵活选择WS-*或WCF实现、代码自动生成、配置的工作。
这两种方式生成的客户端代理都回包括一份重复的Service Contract、Data Contract(还有MessageContract、FaultContact定义),

 

不过就像我们不会在一些大些的项目中直接托拽Connection\Command空间辅助数据访问一样,用这些工具生成的代码、配置文件包括非常多的“垃圾”,
另外,同一份实体编译两次也不利于我们部署。

不妨回归到本来的WCF代码,我们采用类似Remoting的方式,下面是一个瘦身之后的示例:
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
采用该方式的优势:
1、实体一致性, 便于在企业(或行业)内实施全局WCF项目时,标准业务实体重复定义,可以直接服务于行业XML DM(Data Model)或MDM(Master Data Management),可以在多个项目组的Service、Client分发标准业务实体
2、干净,没有“脏”的重复代码和配置
3、符合服务分层结构,
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)

1、Common.dll

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)[DataContract]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
public class Complex
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    [DataMember]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public int X;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    [DataMember]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public int Y;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public Complex(int x, int y)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
this.X = x;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
this.Y = y;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public Complex()
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public override string ToString()
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
return string.Format("({0}, {1})", X, Y);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)}

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)[ServiceContract]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
public interface ICalculator
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    [OperationContract]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
int Add(int x, int y);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    [OperationContract]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
int Substract(int x, int y);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    [OperationContract]
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Complex AddComplex(Complex a, Complex b);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)}

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)


2、Host.exe

 

 

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)public class CalculatorService : ICalculator
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
ICalculator Members#region ICalculator Members
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public int Add(int x, int y)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Console.WriteLine(
"Add");
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
return x + y;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public int Substract(int x, int y)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Console.WriteLine(
"Substract");
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
return x - y;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
public Complex AddComplex(Complex a, Complex b)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Console.WriteLine(
"AddComplex");
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Complex result 
= new Complex();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        result.X 
= a.X + b.X;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        result.Y 
= a.Y + b.Y;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
return result;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
#endregion

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)}

 

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)<?xml version="1.0" encoding="utf-8" ?>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
<configuration>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)  
<system.serviceModel>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
<services>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)      
<service name="Host.CalculatorService">
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        
<endpoint 
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)          
address="http://localhost:8000/Derivatives/Calculator"
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)          binding
="wsHttpBinding"         
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)          contract
="Common.ICalculator" />
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)      
</service>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
</services>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)  
</system.serviceModel>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
</configuration>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)

 

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)static void Main(string[] args)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        host.Open();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Console.WriteLine(
"Service is available.");
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)        Console.ReadKey();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    }

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)}


3、Client.exe

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)<?xml version="1.0" encoding="utf-8" ?>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
<configuration>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)  
<system.serviceModel>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
<client>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)      
<endpoint name="CalculatorService"
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)                      address
="http://localhost:8000/Derivatives/Calculator"
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)                      binding
="wsHttpBinding"
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)                      contract
="Common.ICalculator"/>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    
</client>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)  
</system.serviceModel>
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
</configuration>

 

避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)static void Main(string[] args)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录){
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Console.WriteLine(
"Press any key when the service is available.");
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    ICalculator proxy 
= new ChannelFactory<ICalculator>("CalculatorService").CreateChannel();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Console.WriteLine(proxy.Add(
12));
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Complex a 
= new Complex();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    a.X 
= 3;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    a.Y 
= 4;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Complex b 
= new Complex();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    b.X 
= 3;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    b.Y 
= 1;
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Complex result 
= proxy.AddComplex(a, b);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Console.WriteLine(result);
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)    Console.ReadKey();
避免WCF Service Reference 和 WCF Service Factory的误导作用(摘录)}

转载于:https://www.cnblogs.com/dongpo888/archive/2010/05/24/1742509.html