如何使用 WCF 服务? [英] How can I use a WCF Service?

查看:19
本文介绍了如何使用 WCF 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个新的 WCF 服务时,我可以创建一个这样的新方法:

 [操作合约][WebInvoke(Method = "GET", UriTemplate = "TryThis", ResponseFormat = WebMessageFormat.Json)]字符串 TryThis();

返回简单字符串的函数.

我可以将它与测试客户端 WCF 一起使用,并且有效.但是当我想通过 Chrome、邮递员或我的 Android 应用程序访问我的服务时,我不能.

我尝试使用这些网址:

 http://localhost:54792/Service1.svc/TryThishttp://tempuri.org/IService1/TryThis

当我使用 '

如果有什么我可以帮忙的,请随时告诉我.

When I create a new WCF service, I can create a new method like this one:

 [OperationContract]
 [WebInvoke(Method = "GET", UriTemplate = "TryThis", ResponseFormat = WebMessageFormat.Json)]
 string TryThis();

A function that returns a simple string.

I can use it with the test client WCF and that works. But when I want access to my service with Chrome, postman, or my Android application, I can't.

I tried with theses urls :

 http://localhost:54792/Service1.svc/TryThis
 http://tempuri.org/IService1/TryThis

When I use 'http://localhost:54792/Service1.svc' I have the home page, so this is ok. But I don't have access at any method of my service.

The error is a 400 error. but I don't have any message who indicate where this error is. I don't know if this is the configuration of my IIS server, if this is my service. I'm totally blocked.

This is my web.config :

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
   </appSettings>
    <system.web>
     <compilation debug="true" targetFramework="4.7.1" />
     <httpRuntime targetFramework="4.7.1"/>
    </system.web>
    <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
       </serviceBehaviors>
     </behaviors>
     <protocolMapping>
         <add binding="basicHttpsBinding" scheme="https" />
     </protocolMapping>    
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
   <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
     <directoryBrowse enabled="true"/>
   </system.webServer>
 </configuration>

PS: I already saw this post : "How Can I access WCF services using a Web Reference?" but that didn't help me.

解决方案

It seems that you host the WCF service in IIS, and you want to publish a Restful-style service, you could refer to the following code.
Server:IService.cs

namespace WcfService5
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        string GetData(int value);
    }
}

Server:Service.svc.cs

namespace WcfService5
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

Web.config.

<system.serviceModel>
    <services>
      <service name="WcfService5.Service1">
        <endpoint address="" binding="webHttpBinding" contract="WcfService5.IService1" behaviorConfiguration="MyRest"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyRest">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Result.

Feel free to let me know if there is anything I can help with.

这篇关于如何使用 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆