如何在IIS中没有SVC文件的情况下托管WCF服务

时间:2014-07-02 11:51:23

标签: asp.net web-services wcf rest soap

我希望在IIS中部署双接口(SOAP / REST / XML / JSON)WCF服务,只需配置文件和二进制文件,并且URL中没有svc文件

我们使用VS2012和.Net 4.5

我们有类似的工作,我在这里遵循指南: http://blogs.msdn.com/b/rjacobs/archive/2010/04/05/using-system-web-routing-with-data-services-odata.aspx

我添加了一个带

的Global类
public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        DataServiceHostFactory factory = new DataServiceHostFactory();
        RouteTable.Routes.Add(new ServiceRoute("wrap", factory, typeof(NeOtheWrapper)));
    }
}

我使用现有的web.config来定义所有端点:

<system.serviceModel>
    <!-- Clients -->
    <client>
      <endpoint name="MySoftLive" address="https://backof.somewebsitett.com/Cmp.MySoft.bl/MySoft.svc" binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
      <endpoint name="MySoftTest" address="http://localhost:49957/MySoft.svc"                         binding="basicHttpBinding"  bindingConfiguration="soapBindingConfig"       contract="Cmp.MySoft.BL.WCF.MySoftInterface" />
    </client>
    <!-- Services -->
    <services>
      <service name="Cmp.MySoft.BL.NeOthe.NeOtheWrapper">
        <endpoint name="rest"       address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restBindingConfig"       contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
        <endpoint name="restSecure" address="rest" behaviorConfiguration="restEndpointBehaviour" binding="webHttpBinding"    bindingConfiguration="restSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
        <endpoint name="mex"        address="mex"  behaviorConfiguration=""                      binding="mexHttpBinding"    bindingConfiguration="mexBindingConfig"        contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
        <endpoint name="mexSecure"  address="mex"  behaviorConfiguration=""                      binding="mexHttpsBinding"   bindingConfiguration="mexSecureBindingConfig"  contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
        <endpoint name="soap"       address="soap" behaviorConfiguration=""                      binding="basicHttpBinding"  bindingConfiguration="soapBindingConfig"       contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
        <endpoint name="soapSecure" address="soap" behaviorConfiguration=""                      binding="basicHttpsBinding" bindingConfiguration="soapSecureBindingConfig" contract="Cmp.MySoft.BL.NeOthe.INeOtheWrapper"/>
      </service>
    </services>
    <!--  Binding Configurations -->
    <bindings>
      <webHttpBinding>
        <binding name="restBindingConfig">
          <security mode="None"/>
        </binding>
        <binding name="restSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
      <mexHttpBinding>
        <binding name="mexBindingConfig"/>
      </mexHttpBinding>
      <mexHttpsBinding>
        <binding name="mexSecureBindingConfig"/>
      </mexHttpsBinding>
      <basicHttpsBinding>
        <binding name="soapSecureBindingConfig">
          <security mode="Transport"/>
        </binding>
      </basicHttpsBinding>
      <basicHttpBinding>
        <binding name="soapBindingConfig">
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <!-- Behaviour Configurations -->
    <behaviors>
      <endpointBehaviors>
        <behavior name="restEndpointBehaviour">
          <webHttp helpEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- Hosting Environment Settings -->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>

它编译,运行,如果我浏览http://mypc:12345/wrap/rest/help,我会得到自动生成的ASP.NET REST帮助页面。

但是,如果我去http://mypc:12345/wrap/soap/,我会收到400 Bad Request。

我不能用?wsdl后缀来获取wsdl,或者将url传递给svcutil(soap + xml不是预期的)

我希望显示.SVC SOAP占位符页面,与REST的/ help相同。

如果我浏览.svc文件(它与/ wrap处于同一级别)并且soap服务正常工作,那么元数据发布也是如此。

我使用了错误的网址,还是我的配置错误了?

1 个答案:

答案 0 :(得分:9)

如果您正在使用WCF 4.0或更高版本,则可以使用&#34;无文件&#34;激活。添加以下内容来配置文件:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
  <serviceActivations>
    <add factory="System.ServiceModel.Activation.ServiceHostFactory" 
         relativeAddress="Soap.svc" 
         service="Cmp.MySoft.BL.NeOthe.NeOtheWrapper" />
  </serviceActivations>
</serviceHostingEnvironment>

这允许您在没有物理.svc文件的情况下托管WCF服务。 relativeAddress相对于网站的基地址,您需要像往常一样创建IIS应用程序。

请参阅&#34;无文件激活&#34; A Developer's Introduction to Windows Communication Foundation 4中的部分以获取更多信息。