我们试图使用HTTP GET使用非常非常简单的WCF服务,而我们无法正常工作。我们遵循了那些“指南”,但它不起作用

当我们使用以下URL调用服务时,我们会发现一个页面找不到错误:

http:// localhost:9999/service1.svc/getdata/abc

基本URL(http:// localhost:9999/service1.svc)正常工作,并正确返回WCF服务信息页面。

这些是复制我们示例的步骤和代码。

  1. 在Visual Studio 2010中,创建一个新的“ WCF服务应用程序”项目
  2. 用此代码替换iservice接口

      [ServiceContract()]
      public interface IService1
      {
          [OperationContract()]
          [WebInvoke(Method = "GET", 
                     BodyStyle = WebMessageBodyStyle.Bare, 
                     UriTemplate = "GetData/{value}")]
          string GetData(string value);
      }
    
  3. 用此代码替换服务类

    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
    
  4. web.config看起来像这样

    <system.web>
       <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
      <services>
          <service name="Service1">
              <endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="WebBehavior1">
              </endpoint>
          </service>
      </services>
      <behaviors>
          <endpointBehaviors>
              <behavior name="WebBehavior1">
                 <webHttp helpEnabled="True"/>
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
    

  5. 按Run,尝试调用GET方法

如果有人获得此类似工作,那么如果您可以回复有关工作示例的信息,那将是非常友善的。

非常感谢你

有帮助吗?

解决方案

我重新创建了您的样本 - 像魅力一样工作。

一分:执行您的服务合同(public interface IService1)和服务实施(public class Service1 : IService1)存在于.NET名称空间内?

如果是这样,您需要更改 *.svc和您的 web.config 包括:

<services>
      <service name="Namespace.Service1">
          <endpoint address="" binding="webHttpBinding" 
                    contract="Namespace.IService1" 
                    behaviorConfiguration="WebBehavior1">
          </endpoint>
      </service>
  </services>

<service name="..."> 属性和 <endpoint contract="..."> 必须包括.NET名称空间才能正常工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top