【问题标题】:WCF Json Rest service not hitting the MethodsWCF Json Rest 服务未命中方法
【发布时间】:2012-11-01 08:06:20
【问题描述】:

我正在编写一个 WCF 服务(json REST),并且在使用 wcftestclient.exe 时它运行良好

当我运行该测试工具时,它会在调试时触发我的断点,并且一切都正常工作

但是,当使用浏览器导航到服务和方法时,不会触发断点。似乎请求甚至没有到达代码。

我在使用网络浏览器导航到服务时没有收到任何错误,它只是没有获取任何数据,或者触发断点。

抱歉,如果这是重复的,我已经阅读并尝试了在类似问题的答案中找到的许多不同配置,但似乎没有任何效果。

非常感谢您的帮助,我已经在下面发布了我的代码。

马丁

我有设置: 服务合约

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Country> GetAllCountries();

服务类:

    public List<Country> GetAllCountries()
    {
        ControlServiceRepository rep = new ControlServiceRepository();
        return rep.GetAllCountries().ToList() ;
    }

和我的网络配置

<system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="OmniData" behaviorConfiguration="ServiceConfig">
        <!-- Service Endpoints -->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:55641/"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
      </service>
    </services>
      <behaviors>
        <endpointBehaviors>
          <behavior name="rest">
            <webHttp helpEnabled="true"/>
          </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
          <behavior name="ServiceConfig">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior>
        <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="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

【问题讨论】:

  • 你是从同一个域调用服务吗?
  • 我目前只是想通过 Visual Studio 让它工作,我已经将它上传到 iis7,它是完全一样的。网络浏览器调用是:localhost:55641/OmniData.svc/GetAllStores
  • GetAllStoresGetAllCountries ?
  • 大声笑,上帝帮助我...太长了,GetAllCountries
  • 您可以在测试工具运行时使用Fiddler,看看它如何调用您的服务

标签: c# wcf rest wcf-binding


【解决方案1】:

我认为你的合同中缺少一些东西

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetAllCountries", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Country> GetAllCountries();

试试这个。如果有帮助,请告诉我。

【讨论】:

  • 不幸的是,我几乎有这条确切的线路,我按照许多不同的答案之一将其更改为 WebGet。同样的问题。感谢您的帮助,我现在会尝试任何事情:p
【解决方案2】:

我最终通过删除配置中的所有端点并使用

来完成此工作
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));

如果其他人有问题,这甚至比设置端点更容易,因为您可以在类本身中指定响应类型和端点。

所以:

如果确实存在,请添加一个 global.asax 并包含以下内容:

protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));
        }

装饰你的服务类
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

这是我的:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class OmniData : IOmniData
{
  public Country[] GetAllCountries()
  {
    ControlServiceRepository rep = new ControlServiceRepository();
    return rep.GetAllCountries().ToArray() ;
  }
}

然后是您使用 WebGet 或 WebInvoke 设置端点和类型的界面

public interface IOmniData
    {
        [OperationContract]
        [WebGet(UriTemplate = "OmniData/GetAllCountries", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        Country[] GetAllCountries();
    }

UriTemplate 是终点,因此要访问您将使用的方法:http://MyService.com/OmniData/GetAllCountries

最后是网络配置

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="OmniData">
        <!-- Service Endpoints -->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:55641"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

来自here的很多帮助

但是,重要的是,对于我想要的 json 结果,您需要确保: automaticFormatSelectionEnabled="false" 在那里,因此它将使用界面中指定的响应格式。否则你最终会使用 XML。

希望这对其他人有帮助

再次感谢提琴手!

马丁

【讨论】:

  • 对不起,在哪里添加 "RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));" ??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2016-01-25
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
相关资源
最近更新 更多