C# 将WCF托管到现有MVC网站

C# 将WCF托管到现有MVC网站,c#,asp.net,asp.net-mvc,wcf,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Wcf,Asp.net Mvc 4,我必须做和已经做的事情: 将WCF服务添加到现有mvc web应用程序。在某个文件夹下ex:/Service/Service1.svc 将MVC站点托管到IIS,并对其进行完整的功能测试 namespace WebApplication3.Service { public class Service1 : IService1 { public string DoWork() { return "some string";

我必须做和已经做的事情:

  • 将WCF服务添加到现有mvc web应用程序。在某个文件夹下
    ex:/Service/Service1.svc

  • 将MVC站点托管到IIS,并对其进行完整的功能测试

    namespace WebApplication3.Service
    {
        public class Service1 : IService1
        {
            public string DoWork()
            {
                return "some string";
            }
        }
    }
    
    
    namespace WebApplication3.Service
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method = "GET")]
            string DoWork();
        }
    }
    
  • 和web配置:

    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="transportsecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    
    <services>
      <service name="WebApplication3.Service.Service1" behaviorConfiguration="mybehavior">
        <endpoint address="http://localhost/testsite/Service/Service1.svc" binding="basicHttpBinding" bindingConfiguration="transportsecurity" contract="WebApplication3.Service.IService1">
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="mybehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    
    
    

    当我在IIS上浏览服务时,它显示NotFound错误404


    谢谢

    根据您的托管方式,端点地址很可能不正确/不匹配

    只需将端点地址更改为空字符串

    <services>
      <service name="WebApplication3.Service.Service1" behaviorConfiguration="mybehavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="transportsecurity" contract="WebApplication3.Service.IService1">
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    
    
    
    此外,您已将其配置为https(传输安全),因此请确保您使用的是https url。对于初始测试,我建议首先移除安全性,然后使用基本绑定。一旦它工作,然后测试与安全