【问题标题】:Consume WCF service in windows application?在 Windows 应用程序中使用 WCF 服务?
【发布时间】:2011-08-08 17:34:07
【问题描述】:

我在项目中创建了 WCF 服务。现在使用 Jquery 我可以成功使用服务,但是当我尝试在 Windows 应用程序中添加服务作为 Web 引用时,我无法调用它。如果我创建简单的服务,我可以在 Windows 应用程序中成功调用,但是如果我更改为 jquery 调用服务,我通过谷歌搜索找到了。我不知道我错过了哪里。

下面是代码,我已经完成了。 这是 WCF 的接口。

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    void DoWork();

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]    
    string HelloWorld();
}

类调用接口

    public class TestService : ITestService
{
    public void DoWork()
    {
    }


    public string HelloWorld()
    {
        return "Hello World";
    }
}

}

这是应用程序的网络配置。

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="TestWCFBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="TestWCFBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
  <services>
    <service name="WebAppWithWCF.TestService" behaviorConfiguration="TestWCFBehavior">
      <endpoint address="" binding="webHttpBinding" contract="WebAppWithWCF.ITestService" behaviorConfiguration="TestWCFBehavior"/>
    </service>
  </services>
</system.serviceModel>

我做了一些尝试和错误,但直到现在都找不到方法......

请帮助...如果我身边缺少任何信息,请告诉我。

【问题讨论】:

    标签: jquery asp.net wcf window


    【解决方案1】:

    webHttpBinding 不公开元数据,因此您无法添加对它的引用。

    基本上,添加服务引用功能会根据已知的服务(通过元数据/WDSL公开)生成代理和类。

    带有 wsHttpBinding 或 basicHttpBinding 的服务都会公开服务的元数据。它们通常在您构建 .NET 到 .NET 客户端-服务器应用程序时使用。

    实现两个绑定:WCF Service with webHttpBinding-basicHttpBinding-wcHttpBinding

    【讨论】:

    • 我们可以添加多个绑定,这在我们想要暴露多个事物时非常方便。谢谢马克斯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多