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

Rob*_*J1M 5 asp.net rest wcf soap web-services

我想在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)));
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用现有的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>
Run Code Online (Sandbox Code Playgroud)

它编译,运行,如果我浏览到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服务正常工作,那么元数据发布也是如此.

我使用了错误的URL还是我的配置错了?

Tim*_*Tim 9

如果您使用的是WCF 4.0或更高版本,则可以使用"无文件"激活.添加以下内容来配置文件:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
  <serviceActivations>
    <add factory="System.ServiceModel.Activation.ServiceHostFactory" 
         relativeAddress="Soap.svc" 
         service="Cmp.MySoft.BL.NeOthe.NeOtheWrapper" />
  </serviceActivations>
</serviceHostingEnvironment>
Run Code Online (Sandbox Code Playgroud)

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

有关详细信息,请参阅A Developer的Windows Communication Foundation 4简介中的"无文件激活"部分.