6

私はしばらくの間.NETで作業してきましたが、WCFは初めてです。JSONを使用して最初のWCFサービスを作成しようとしています。本当に、本当にシンプルに始めて、そこから構築しようと思いました。しかし、私はどういうわけか、最も単純なサービスでさえも台無しにすることができました。これが私がこれまでに得たものです。

Web.Config:

   <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MarathonInfo.MarathonInfoService">
        <endpoint address="http://localhost:10298/MarathonInfoService.svc" binding="webHttpBinding" contract="MarathonInfo.IMarathonInfo" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

次に、サービスファイルで:

namespace MarathonInfo
{
    public class MarathonInfoService : IMarathonInfo
    {
        public String GetData()
        {
            return "Hello World";
        }
    }
}

そしてインターフェースで:

namespace MarathonInfo
{
    [ServiceContract]
    public interface IMarathonInfo
    {

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        String GetData();
    }
}

だから、私がこのURLに行くとき:

http://localhost:10298/MarathonInfoService.svc/GetData

このエラーが発生します:

EndpointDispatcherでのAddressFilterの不一致が原因で、To'http:// localhost:10298 / MarathonInfoService.svc/GetData'のメッセージを受信者で処理できません。送信者と受信者のEndpointAddressesが一致していることを確認します。

VisualStudioをデバッグモードで使用してサービスを正常に実行できます。しかし、ブラウザでは、そのエラーしか表示されません。

私は何が間違っているのですか?

ありがとう!

ケーシー

4

1 に答える 1

16

WCF WebHTTPエンドポイント(つまり、JSONを返し、[WebGet] / [WebInvoke]属性を使用するエンドポイント)を作成する場合は、エンドポイントに<webHttp/>関連付けられた動作を設定する必要があります。

<system.serviceModel> 
  <services> 
    <service name="MarathonInfo.MarathonInfoService"> 
      <endpoint address="http://localhost:10298/MarathonInfoService.svc"
                binding="webHttpBinding"
                contract="MarathonInfo.IMarathonInfo"
                behaviorConfiguration="Web"/> 
    </service> 
  </services> 
  <behaviors> 
    <serviceBehaviors> 
      <behavior> 
        <serviceMetadata httpGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="Web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors> 
  <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> 
</system.serviceModel> 
于 2012-05-26T13:59:58.950 に答える