简体   繁体   English

WCF-从客户端和服务器端调用相同的服务

[英]WCF - call same service from client and server side

I have a simple WCF service that I call server side from code behind via a service reference. 我有一个简单的WCF服务,可以通过服务引用从后面的代码中调用服务器端。 It's used for validation and works and was automatically setup by Visual Studio and is using SOAP I think because the binding is wsHttpBinding. 它用于验证和工作,并且由Visual Studio自动设置,并且我认为使用SOAP是因为绑定是wsHttpBinding。

I want to use the same WCF service, but call it client side from jQuery using ajax(). 我想使用相同的WCF服务,但使用ajax()从jQuery称为客户端。 I'm trying to implement it by way of these instructions . 我正在尝试通过这些说明来实现它。

But if I make the changes to get the client side call working, I have to add the decoration below which I think will break what works on the server side and also change the system.serviceModel section in web.config. 但是,如果我进行了更改以使客户端调用能够正常工作,则必须添加修饰符,在该修饰符之下,我认为这将破坏服务器端的作用,并更改web.config中的system.serviceModel部分。

[WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat= WebMessageFormat.Json) ]

How do I have a WCF service that can be called both from the server-side and client side (jQuery/ajax)? 我如何拥有可以从服务器端和客户端(jQuery / ajax)调用的WCF服务?

Or put another way, this may be incorrectly worded, can an endpoint have multiple bindings? 或者换种说法,这可能是措辞不正确,端点可以有多个绑定吗?

An endpoint in WCF has an ABC WCF中的端点具有ABC

  • Address 地址
  • Binding 捆绑
  • Contract 合同

So therefore: no, a single endpoint cannot have multiple bindings. 因此,因此:不,单个端点不能具有多个绑定。 But you can have multiple endpoints for the same contract! 但是同一合约可以有多个端点!

So if you want to expose two methods with basically the same functionality over SOAP and REST, you need to have two endpoints. 因此,如果要通过SOAP和REST公开具有基本相同功能的两个方法,则需要具有两个端点。

You can define your own service method to be callable both from SOAP and REST as shown in that link you mentioned. 您可以定义自己的服务方法,以使其可以从SOAP和REST调用,如您所提到的链接所示。

What you then need are two separate endpoints: 然后,您需要两个单独的端点:

<services>
   <service name="YourService">
     <endpoint
           address="http://YourServer/YourVirtualDir/YourService.svc"
           binding="wsHttpBinding"
           contract="IYourService" />
     <endpoint
           address="http://YourServer/YourVirtualDir/YourService2.svc"
           binding="webHttpBinding"
           behaviorConfiguration="webCallable"
           contract="IYourService" />
   </service>
</services>

You'll have to define an endpoint behavior that includes the <webHttp/> behavior - or you need to have a *.svc file that uses the WebServiceHost (instead of plain ServiceHost ) to host your REST service. 您必须定义一个包含<webHttp/>行为的终结点行为-或者您需要具有一个* .svc文件,该文件使用WebServiceHost (而不是普通的ServiceHost )来承载REST服务。

As you can see, you now have two separate endpoints (with two separate *.svc files in a vritual directory), two separate addresses - but both share the same contract (define the same operations). 如您所见,您现在有两个单独的终结点(虚拟目录中有两个单独的* .svc文件),两个单独的地址-但它们共享相同的协定(定义相同的操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM