IIS不会激活WCF服务,404错误

时间:2013-10-03 15:20:38

标签: c# wcf iis-7.5

我有一个只会公开HTTP端点的WCF服务,我的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端点,这很好:这是我想要的那个方面的行为;当我尝试POST到http://localhost/Project/ServiceOne/ServiceMethod时出现问题。该方法存在于服务合同和实现中,并且在界面中也作为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();
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您需要设置正确的行为:

<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>
相关问题