繁体   English   中英

IIS无法激活WCF服务,出现404错误

[英]IIS won't activate WCF Service, 404 error

我有一个WCF服务,它将仅公开HTTP端点,我的App.config是这样的:

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="traceListener" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "C:\Users\Developer\Documents\ProjectName\Servicelog.svclog" />
            </listeners>
         </source>
      </sources>
   </system.diagnostics>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
      <service name="Project.ServiceOne">
        <endpoint address="http://localhost/Project/ServiceOne" binding="webHttpBinding" contract="Project.IServiceOne"/>
      </service>
      <service name="Project.ServiceTwo">
        <endpoint address="http://localhost/Project/ServiceTwo" binding="webHttpBinding" contract="Project.IServiceTwo"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
   <httpProtocol>
     <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept"/>
     </customHeaders>
   </httpProtocol>
  </system.webServer>
  <startup>
    <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

我正在将IIS 7.5(Windows 7 x64-完整的程序和功能,而不是Express)运行到默认网站(项目应用程序)。

我可以浏览到.svc文件,它会告诉我未启用MEX端点,这很好。 当我尝试过帐到http://localhost/Project/ServiceOne/ServiceMethod 该方法存在于Service合同和实现中,并且在界面中也以WebInvoke的形式进行修饰,但是对其进行POST操作将仅返回HTTP 404。 用GET装饰测试方法并浏览到该方法会导致MapRequestHandler提供404服务。

我的App.config文件出了什么问题? 我如何使这些端点正常工作?

根据要求,接口:

[ServiceContract]
public interface IServiceOne
{
    [OperationContract]
    [WebInvoke(Method = "GET",
       RequestFormat = WebMessageFormat.Json,
       ResponseFormat = WebMessageFormat.Json,
       UriTemplate = "MyMethod")]
    List<String> MyMethod();
}

本文帮助我设置了第一个WCF服务,请阅读:

http://www.mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

您需要设置适当的行为:

<services>
  <service name="Project.ServiceOne" behaviorConfiguration="serviceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="Project.IServiceOne" behaviorConfiguration="webBehaviour"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
    <host>
      <baseAddresses>
        <add baseaddress="http://localhost/Project/ServiceOne">
      </baseAddresses>
    </host>
  </service>
  <service name="Project.ServiceTwo" behaviorConfiguration="serviceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="Project.IServiceTwo" behaviorConfiguration="webBehaviour"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
    <host>
      <baseAddresses>
        <add baseaddress="http://localhost/Project/ServiceTwo">
      </baseAddresses>
    </host>
  </service>    
</services>
<behaviors>
    <serviceBehaviors>
        <behavior name="serviceBehavior">         
          <serviceMetadata httpGetEnabled="true"/>        
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
    </endpointBehaviors>
</behaviors>

暂无
暂无

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

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