将WCF服务转换为RESTful应用程序

本文关键字:RESTful 应用程序 转换 WCF 服务 | 更新日期: 2023-09-27 18:28:50

嘿,我把wcf变成一个restful服务没有任何进展。所以我想知道当你在这里启动WCF服务应用程序时,是否有人可以使用基本代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

以及服务:

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

我所做的就是启动这个WCF服务应用程序,并用一个基本表单打开另一个VS2010解决方案,该表单有一个文本框按钮和标签,并复制了该服务应用程序在另一个解决方案中的服务位置,所以当我键入一个数字时,我会收到该服务的响应。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        public ServiceReference1.Service1Client testClient = new ServiceReference1.Service1Client();
        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = testClient.GetData(Convert.ToInt32(textBox1.Text));
        }
    }
}

真的很快,很脏,但达到了目的。

现在,如果有人可以帮助编写代码,你如何将其转化为一个restful服务?

我的配置文件的结束部分:

 <system.serviceModel>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <!-- 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>
  </system.serviceModel>
</configuration>

使用Justins配置代码编辑更新:

因此,当我触摸配置文件时,我通常的错误是:添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据

Error: Cannot obtain Metadata from http://localhost:26535/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:26535/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:26535/Service1.svc'.    The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.HTTP GET Error    URI: http://localhost:26535/Service1.svc    There was an error downloading 'http://localhost:26535/Service1.svc'.    The request failed with the error message:--<html>    <head>        <title>Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'. ÿVariables for UriTemplate path segments must have type 'string'.</title>        <style>         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}          p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }         pre {font-family:"Lucida Console";font-size: .9em}         .marker {font-weight: bold; color: black;text-decoration: none;}         .version {color: gray;}         .error {margin-bottom: 10px;}         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }        </style>    </head>    <body bgcolor="white">            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>            <h2> <i>Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'. ÿVariables for UriTemplate path segments must have type 'string'.</i> </h2></span>            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.            <br><br>            <b> Exception Details: </b>System.InvalidOperationException: Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'. ÿVariables for UriTemplate path segments must have type 'string'.<br><br>            <b>Source Error:</b> <br><br>            <table width=100% bgcolor="#ffffcc">               <tr>                  <td>                      <code>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code>                  </td>               </tr>            </table>            <br>            <b>Stack Trace:</b> <br><br>            <table width=100% bgcolor="#ffffcc">               <tr>                  <td>                      <code><pre>[InvalidOperationException: Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'.  Variables for UriTemplate path segments must have type 'string'.]   System.ServiceModel.Dispatcher.UriTemplateClientFormatter.Populate(Dictionary`2& pathMapping, Dictionary`2& queryMapping, Int32& totalNumUTVars, UriTemplate& uriTemplate, OperationDescription operationDescription, QueryStringConverter qsc, String contractName) +726   System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter..ctor(OperationDescription operationDescription, IDispatchMessageFormatter inner, QueryStringConverter qsc, String contractName, Uri baseAddress) +94   System.ServiceModel.Description.WebHttpBehavior.GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) +137   System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) +659   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +3864   System.ServiceModel.ServiceHostBase.InitializeRuntime() +37   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +49   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +261   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +121   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +479[ServiceActivationException: The service '/Service1.svc' cannot be activated due to an exception during compilation.  The exception message is: Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'.  Variables for UriTemplate path segments must have type 'string'..]   System.ServiceModel.AsyncResult.End(IAsyncResult result) +11655726   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +194   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +176   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +275   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75</pre></code>                  </td>               </tr>            </table>            <br>            <hr width=100% size=1 color=silver>            <b>Version Information:</b>ÿMicrosoft .NET Framework Version:2.0.50727.5448; ASP.NET Version:2.0.50727.5456            </font>    </body></html><!-- [InvalidOperationException]: Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'.  Variables for UriTemplate path segments must have type 'string'.   at System.ServiceModel.Dispatcher.UriTemplateClientFormatter.Populate(Dictionary`2& pathMapping, Dictionary`2& queryMapping, Int32& totalNumUTVars, UriTemplate& uriTemplate, OperationDescription operationDescription, QueryStringConverter qsc, String contractName)   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter..ctor(OperationDescription operationDescription, IDispatchMessageFormatter inner, QueryStringConverter qsc, String contractName, Uri baseAddress)   at System.ServiceModel.Description.WebHttpBehavior.GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)   at System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)   at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)   at System.ServiceModel.ServiceHostBase.InitializeRuntime()   at System.ServiceModel.ServiceHostBase.OnBeginOpen()   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(String normalizedVirtualPath)   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)[ServiceActivationException]: The service '/Service1.svc' cannot be activated due to an exception during compilation.  The exception message is: Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'.  Variables for UriTemplate path segments must have type 'string'..   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext)   at System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e)   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)-->--.

将WCF服务转换为RESTful应用程序

正如您所提到的,这是一个新项目,因此重定向可能有助于使过程变得简单一点?

这是MSDN上的一篇文章,介绍您的问题。

然而,我特别建议查看ServiceStack来创建RESTful服务,因为它使过程变得非常简单。WCF绝对没有提供一种简单的方法来实现这一点。在这种情况下,他们过于复杂了

如果这真的是项目的开始,我会选择这一点

本文给出了一个更直接的答案,这篇文章有点老,但可能有助于理解REST,以及如何在WCF中实现它。并且是在Web[Type]属性中指定GET/POST/PUT/DELETE

[WebGet(UriTemplate = @"Data?value={value}")]
[OperationContract]
string GetData(int value);

此外,您还需要在应用程序的.config中执行此操作(同样适用于Skonnard的旧MSDN文章)

<configuration>
   <system.serviceModel>
     <services>
        <service name="Service1">
            <endpoint binding="webHttpBinding" contract="Service1"
                      behaviorConfiguration="webHttp"/>
        </service>
     </services>
     <behaviors>
        <endpointBehaviors>
            <behavior name="webHttp">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
     </behaviors>
  </system.serviceModel>
<configuration>

这将转化为您的配置:

<system.serviceModel>
<services>
  <service name="WcfService1.Service1"
        behaviorConfiguration="WcfService1.Service1Behavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1"
        behaviorConfiguration="webHttp">
      <!-- 
          Upon deployment, the following identity element should be removed 
          or replaced to reflect the identity under which the deployed service runs.  
          If removed, WCF will infer an appropriate identity automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="WcfService1.Service1Behavior">
      <!-- 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>
  <endpointBehaviors>
      <behavior name="webHttp">
           <webHttp/>
      </behavior>
  </endpointBehaviors>
</behaviors>

然后,您可能需要向端点添加一个地址属性,以便它知道在哪里查找。

在配置一个简单的WCF REST服务时,过去我遇到过类似于您遇到的错误。在Justin提到的文章中:
使用WCF 3.5设计和构建RESTful Web服务指南
(搜索-定义HTTP接口:[WebGet]

您会注意到Get方法都接受字符串。

当您尝试将一个示例WCF项目转换为RESTful项目时,出现的错误很常见。要修复此问题,只需更改方法和接口的签名以接受string,而不是int。这就是内部异常所抱怨的:

约定"IService1"中的操作"GetData"具有名为"value"的路径变量,该变量的类型不为"string"。UriTemplate路径段的变量必须具有类型"string"

原件:

public string GetData(int value)

修改:

public string GetData(string value)

下面是我的一个示例项目中的一个简单.config部分,我知道它很有效:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Service1Behavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="Wcf.Service1">
        <endpoint address=""
                behaviorConfiguration="Service1Behavior"
                binding="webHttpBinding"
                contract="Wcf.IService1"/>
      </service>
    </services>
  </system.serviceModel>

在希望作为RESTful服务可用的操作上使用WebGet属性。

使用webHttpBinding。

记得在配置中添加您的行为。

应该足够开始了。

编辑:

在您的服务下添加新的绑定:

<service><endpoint binding="webHttpBinding"... behavior="myBehavior"/> 

等等-然后再添加

<behavior name="myBehavior"><webHttp/></behavior>

作为端点行为。

[WebGet]属性-查看各种选项以进一步开发它。

这是来自codeproject.com的示例项目。需要制作

[WebGet(UriTemplate = "?id={id}")]

而不是

[WebGet(UriTemplate = "{id}")]