我已经采取了发展上的.NET WCF项目。除此之外,该项目包含3个文件:
I have taken over development on a .NET WCF project. Among other things, the project contains 3 files:
IApi.cs< =
定义接口
JsonApi.svc.cs< =
实现接口JSON
SoapApi.svc.cs< =
实现接口SOAP
IApi.cs <=
Defining the interfaces
JsonApi.svc.cs <=
Implementing interface for JSON
SoapApi.svc.cs <=
Implementing interface for SOAP
这两个执行文件几乎是相同的 - 至少所有的code中的方法的实现是相同的。我是很新的WCF编程,但是这让我觉得奇怪,我们需要复制code,只是为了实现JSON和SOAP。
The two implementation files are almost identical - at least all the code in the implementation of the methods is identical. I am quite new to WCF programming, but it strikes me as odd, that we need to duplicate the code, just to implement JSON as well as SOAP.
有没有办法合并到这一种实现方式,并让框架决定,如果数据是通过SOAP或JSON运?
Is there a way to merge this into one implementation and let the framework decide if data is to be transported by SOAP or JSON?
/卡斯滕
定义两个端点,用同样的合同,您的服务实现。定义首先使用SOAP,那么第二个使用JSON:
Defines two endpoints, with the same contract for your service implementation. Defines the first to use SOAP, then the second to use JSon :
<service name="YourService">
<endpoint address="rest"
binding="webHttpBinding"
contract="IYourService"
behaviorConfiguration="RestBehavior"/>
<endpoint address="soap"
binding="wsHttpBinding"
contract="IYourService"/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
<endpointBehaviors>
<behavior name="RestBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
接下来将在 HTTP端点://.../yourservice.svc/soap ,另一个在 HTTP://.../yourservice.svc/rest
回答您的评论,我说的是,以取代本节:
[edit] to answer to your comment, what I said is to replace this section :
<services>
<service name="WebApi.SoapApi" behaviorConfiguration="ApiBehavior">
<endpoint address="basic" bindingNamespace="http://api.myservice.dk/Basic" contract="WebApi.IApi" binding="basicHttpBinding" bindingConfiguration="ApiBinding" />
</service>
<service name="WebApi.JsonApi" behaviorConfiguration="ApiBehavior">
<endpoint address="web" bindingNamespace="http://api.myservice.dk/Web" contract="WebApi.IApi" binding="webHttpBinding" bindingConfiguration="ApiBinding" behaviorConfiguration="JsonBehavior" />
</service>
</services>
是:
<services>
<service name="WebApi.UniqueApi" behaviorConfiguration="ApiBehavior">
<endpoint address="basic" bindingNamespace="http://api.myservice.dk/Basic" contract="WebApi.IApi" binding="basicHttpBinding" bindingConfiguration="ApiBinding" />
<endpoint address="web" bindingNamespace="http://api.myservice.dk/Web" contract="WebApi.IApi" binding="webHttpBinding" bindingConfiguration="ApiBinding" behaviorConfiguration="JsonBehavior" />
</service>
</services>
一条龙服务,有两个端点
One service, with two endpoints