2016-08-04 97 views
0

我对WCF服务有点新。在我目前,我已经开发了一个hello world的Restful WCF服务。以下是我的RESTful Web服务的代码。通过GET请求和响应将对象传递给WCF服务

的RESTful服务合同如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace RestfulWCFService 
{ 
    [ServiceContract] 
    public interface IRestfulTestService 
    { 
     [OperationContract] 
     [WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Xml, UriTemplate="xml/{name}")] 
     string SayHelloXml(string name); 

     [OperationContract] 
     [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{name}")] 
     string SayHelloJson(string name); 
    } 
} 

接口实现如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace RestfulWCFService 
{ 

    public class RestfulTestService : IRestfulTestService 
    { 


     string IRestfulTestService.SayHelloXml(string name) 
     { 
      return "Hello " + name; 
     } 

     string IRestfulTestService.SayHelloJson(string name) 
     { 
      return "Hello " + name; 
     } 
    } 
} 

我已经在IIS上部署该服务,现在我访问使用该Web服务以下URL。

http://localhost/RestfulWCFService/RestfulTestService.svc/xml/pankesh

的web服务返回我下面的数据。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello pankesh</string> 

现在,我的问题是 - 在上面的web服务,我传递一个简单的数据类型是STRING。现在,我的要求是我想通过REST请求传递一个复杂的自定义对象。您可以请教我 - 如何通过上述场景中的REST请求传递自定义对象?

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.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="RestfulWCFService.RestfulTestService"> 
     <endpoint behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="" contract="RestfulWCFService.IRestfulTestService" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="WebBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <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" /> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true" /> 
    </system.webServer> 

</configuration> 

回答

0
  1. 连载对象到流
  2. 通作为流
  3. 反序列化到合同数据
    Serializing/deserializing with memory stream
+0

到服务感谢指针。但是,我的要求是我想直接将整个对象传递给浏览器URL上的GET请求。我不想写一个客户端。在这里,我的客户端界面是浏览器URL。 – Pankesh

+0

http://stackoverflow.com/questions/8336807/how-to-pass-object-to-restful-service-with-get-request –

+0

https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/ –