2013-06-05 2 views
0

Я пишу службу WCF. Вот реализация сторона ServiceПолучение нулевых значений в службе post wcf

[OperationContract(Name="GetMediaFile")] 
    [Description(ServiceDescConstants.GetMediaFile)] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = UriTemplateConstants.GetMediaFile)]   
    Stream GetMediaFile(string type, string mediaId); 

где

UriTemplateConstants.GetMediaFile = "GetMediaFile?type={type}&mediaId={mediaId}"; 

Вот реализация метода интерфейса

public Stream GetMediaFile(string type, string mediaId) 
    { 
      CustomerBL customerBl = new CustomerBL(); 
      return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId));    
    } 

На стороне клиента я использую RestClient плагин для тестирования сервиса. Вот данные я направляю

URL: customersite/GetMediaFile Заголовок: Content-Type = х-WWW-форм-urlencoded Body: тип = 0 & MediaId = 1

Любая помощь !!

Теперь проблема заключается в том, что я получаю нулевые значения

ответ

2

Измените метод интерфейса:

[OperationContract(Name = "GetMediaFile")] 
[Description(ServiceDescConstants.GetMediaFile)] 
[WebInvoke(Method = "POST", 
    ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    UriTemplate = "/GetMediaFile")] 
Stream GetMediaFile(Stream input); 

А также изменить его реализации:

public Stream GetMediaFile(Stream input) 
{ 
    StreamReader sr = new StreamReader(input); 
    string s = sr.ReadToEnd(); 
    sr.Dispose(); 
    NameValueCollection qs = HttpUtility.ParseQueryString(s); 
    string type = qs["type"]; 
    string mediaId = qs["mediaId"]; 

    CustomerBL customerBl = new CustomerBL(); 
    return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId)); 
} 

В web.config , используйте следующую конфигурацию (убедитесь, что вы используете собственные имена пространств имен/классов/интерфейсов):

<system.serviceModel> 
    <services> 
    <service name="WcfServices.MyService"> 
     <endpoint address="" 
       name="webEndPoint" 
       behaviorConfiguration="webBehavior" 
       binding="webHttpBinding" 
       contract="WcfServices.IMyService" /> 
    </service> 
    </services> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="webBehavior"> 
     <webHttp /> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 

Это запрос образца:

POST /MyService.svc/GetMediaFile HTTP/1.1 
Host: localhost:64531 
Cache-Control: no-cache 
Content-Type: application/x-www-form-urlencoded 

type=0&mediaId=1 

Решением является адаптацией блог статьи Edgardo ROSSETTO в, Raw HTTP POST with WCF.

+0

Thanx это работает .. – user2061951

Смежные вопросы