2016-08-04 101 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/ –