C# 创建WCF服务

C# 创建WCF服务,c#,wcf,C#,Wcf,我是WCF的新手。我已经创建了一个wcf服务应用程序,其中有一个接口和类 我的界面: using System.ServiceModel; namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] string Display(); } } 我的班级: namespace Wc

我是WCF的新手。我已经创建了一个wcf服务应用程序,其中有一个接口和类

我的界面:

using System.ServiceModel;

namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string Display();        
    }    
}
我的班级:

namespace WcfService1
{
    public class Service1 : IService1
    {
        public string Display()
        {
            return "Hello";
        }
    }
}
现在,当我在浏览器中运行此服务时。单击Service1.svc后将出现以下屏幕

如何访问浏览器上的Display()

在遵循第一个答案之后,接下来是:


如果您想通过浏览器访问WebGet,我想您正在寻找它

using System.ServiceModel;
using System.ServiceModel.Web;

namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.XML, ResponseFormat = WebMessageFormat.XML, UriTemplate = "/display/")]
        string Display();        
    }    
}
加上这些行为

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
然后它应该以XML格式显示一个字符串(如果愿意,可以将其更改为JSON)

此外,请确保引用以下库

System.ServiceModel.Web
System.Web.Extensions
System.Web.Services
System.Web

将解决方案分为两个项目:

  • 新建项目->WCF->WCF服务库
  • 新建项目->WCF->WCF服务应用程序
“服务应用程序”是包含svc端点的web项目。 “服务库”是包含服务的类库

当您运行(F5)服务库项目时,VisualStudio将启动WCF测试客户端(WcfTestClient.exe),为您提供测试服务的快速界面


web项目中的.svc端点没有为您提供内置测试界面(与asmx时代的工作方式类似)。

请尝试
Service1.svc/Display
@EhsanSajjad i have try”“。但是没有发生任何事情,您必须创建一个单独的项目,一个客户端,它将连接到服务。这方面的一个例子,如果显示在您的qyestion附带的屏幕打印中。如果您也想通过浏览器访问您的服务方法,那么我认为Web API更适合您的需要。@Maarten您的意思是我不能在浏览器中访问Display()方法。这是您想要的restful服务的一种功能,因此您应该遵循@ramie的答案。我跟进了您的代码。它不起作用。请参见上面的屏幕快照我已将XML更改为JSON。。现在它正在回复HELLO。谢谢
<bindings>
  <webHttpBinding>
    <binding name="b_WebHttpBinding" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered"
        useDefaultWebProxy="true" crossDomainScriptAccessEnabled="true">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
http://localhost:22727/Service1.svc/display/
System.ServiceModel.Web
System.Web.Extensions
System.Web.Services
System.Web