简体   繁体   English

WCF服务上的多个绑定

[英]Multiple Bindings on WCF Service

I want to host a WCF 4.0 Service in IIS 7.5, and be able to bind to it with basicHttpBinding and also RESTfully with webHttpBinding . 我想在IIS 7.5中托管WCF 4.0服务,并且能够使用basicHttpBinding绑定到它,并且还可以使用webHttpBinding RESTful绑定。

I need to be able to access it like so: 我需要能够像这样访问它:

http://server/wcf/service/method/parameters (REST) http:// server / wcf / service / method / parameters (REST)

and also like so: 并且如此:

http://server/wcf/service.svc (Basic HTTP) http://server/wcf/service.svc (基本HTTP)

So far, I have this for my Web.config: 到目前为止,我有我的Web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp defaultOutgoingResponseFormat="Json" helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="SAIF.Services.WCF.Services.CustomerContactService">
        <endpoint address="CustomerContact" behaviorConfiguration="json" binding="webHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
        <endpoint address="CustomerContact.svc" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.ICustomerContactService" />
      </service>
      <service name="SAIF.Services.WCF.Services.OnlineLoginService">
        <endpoint address="OnlineLogin" binding="basicHttpBinding" contract="SAIF.Services.WCF.Contracts.IOnlineLoginService" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
      <serviceActivations>
        <add relativeAddress="CustomerContact.svc" service="SAIF.Services.WCF.Services.CustomerContactService" />
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
</configuration>

I also have this in my global.asax file for the extension less activation's: 我在我的global.asax文件中也有这个扩展名为less activation的:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
        Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContact", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.CustomerContactService)))
        Routing.RouteTable.Routes.Add(New ServiceRoute("OnlineLogin", New ServiceHostFactory, GetType(SAIF.Services.WCF.Services.OnlineLoginService)))
    End Sub

I have decorated the service's with this: 我用这个装饰了服务:

And my Service Interface's with the UriTemplates 我的服务界面与UriTemplates

Don't seem to be able to access them both RESTfully and over SOAP. 似乎无法通过REST和SOAP访问它们。

Thanks! 谢谢! Sam 山姆

Just decorate your method with both OperationContract and WebGet attributes. 只需使用OperationContract和WebGet属性修饰您的方法。 Now add the following to system.serviceModel element in your servers web.config 现在将以下内容添加到服务器web.config中的system.serviceModel元素中

    <standardEndpoints>
          <webHttpEndpoint>
            <!-- 
                Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
                via the attributes on the <standardEndpoint> element below
            -->
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true">
              <readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
            </standardEndpoint>
          </webHttpEndpoint>
        </standardEndpoints>

NOTE: Yo You can remove the json endpoint from the above(as we would achieve a clear URL concept using the new REST Api), And in your global.asax just replace the following: 注意:哟您可以从上面删除json端点(因为我们将使用新的REST Api实现清晰的URL概念),并且在您的global.asax中只需替换以下内容:

Routing.RouteTable.Routes.Add(New ServiceRoute("CustomerContactService", New WebServiceHostFactory, typeof(SAIF.Services.WCF.Services.CustomerContactService)));

Now once you do the above you should be able to access the same service via SOAP and REST and the URLs would be as follows: 现在,一旦执行上述操作,您就可以通过SOAP和REST访问相同的服务,URL将如下所示:

SOAP --> http://localhost/virtualdirectoryname/CustomerContactService.svc SOAP - > http://localhost/virtualdirectoryname/CustomerContactService.svc

REST --> http://localhost/virtualdirectoryname/CustomerContactService/method/parameters REST - > http:// localhost / virtualdirectoryname / CustomerContactService / method / parameters

Now browse to your service in IE and you should see the SOAP server when you browse to .svc file and when you browse to the rest URL you should see either xml coming up in the browser of a file should be downloaded that contains the response in json format. 现在浏览到您在IE中的服务,当您浏览到.svc文件时应该看到SOAP服务器,当您浏览到其余URL时,您应该看到应该下载包含响应的文件浏览器中的xml。 json格式。

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

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