只能以 SVCUtil 或使用VS2005 來產生 proxy file 嗎?
這樣對開發是一個很痛苦的折磨。
在Client side,通常也會使用與 Server side 相同的 business entity。對於同一個開發平台來說,這是非常重要的事情。
但如果使用 svcutil 來產生 proxy class,就會重複的產生 client side 的 business entity。非常不道德。
經過微軟的Patrick 的努力,幫我找到了使用 Custom Channel 的方法。而且,可以直接 reference 與server side 相同的 service contract, business entity。這對於開發來說,至為重要。
其實 code 相當簡單。
// Business entity: Server side, Client side 共用
[DataContract]
public class Customer
{
public Customer(int customId, string name)
{
this.customId = customId;
this.name = name;
}
private string name;
[DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
private int customId;
[DataMember]
public int CustomId
{
get { return customId; }
set { customId = value; }
}
}
//Business Component: Server side
public class CustomBC
{
public Customer GetFirstCustomer()
{
Customer c = new Customer(1, "Charles");
return c;
}
}
//Contract interface: Server side, client side 共用
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
Customer GetFirstCustomer();
}
//Contract Implementation: Server side
[ServiceBehavior(Name = "CustomerService", Namespace = "http://TestCustomChannel.ServiceContracts/2007/05")]
public class MathService : TestCustomChannel.ServiceContracts.ICustomerService
{
public TestCustomChannel.BusinessEntities.Customer GetFirstCustomer()
{
CustomBC bc = new CustomBC();
return bc.GetFirstCustomer();
}
}
//Client Side: 引用Contract interface, business entity
ICustomerService proxy = new ChannelFactory< ICustomerService >("BasicHttpBinding_ICustomerService").CreateChannel();
Customer c = proxy.GetFirstCustomer();
//Client Side config
<client>
<endpoint address="http://localhost:4122/TestCustomChannel.Host/CustomerService.svc"
binding="basicHttpBinding"
contract="TestCustomChannel.ServiceContracts.ICustomerService"
name="BasicHttpBinding_ICustomerService"
/>
</client>
沒有留言:
張貼留言