2014-07-30 3 views
0
[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "/v1/getCustomBodyTypes", ResponseFormat = WebMessageFormat.Json)] 
    [JSONPBehavior(callback = "callback")] 
    List<String> v1_GetCustomBodyType(); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    [JSONPBehavior(callback = "callback")] 
    Response v1_AddCustomBodyType(); 
} 

После реализации интерфейса и размещения службы WCF в IIS я мог бы вызвать OperationContract сверху с клиента.Не удалось вызвать метод WebInvoke с параметром

Но при добавлении параметра для него, как следующее, я мог бы вызвать v1_GetCustomBodyType, но я встретил следующее сообщение об ошибке при вызове v1_AddCustomBodyType «Базовое соединение закрыто: Непредвиденная ошибка приема»

[DataContract] 
public class CustomBodyType 
{ 
    [DataMember] 
    public string Redbook { get; set; } 
    [DataMember] 
    public string CustomBodyStyle { get; set; } 
} 

[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "/v1/getCustomBodyTypes", ResponseFormat = WebMessageFormat.Json)] 
    [JSONPBehavior(callback = "callback")] 
    List<String> v1_GetCustomBodyTypes(); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    [JSONPBehavior(callback = "callback")] 
    Response v1_AddCustomBodyType(CustomBodyType data); 
} 

я называю обслуживание через WebRequest

Исходный код JSONPBehavior:

public class JSONPBehavior : Attribute, IOperationBehavior 
{ 
    public string callback; 
    #region IOperationBehavior Members 
    public void AddBindingParameters(
     OperationDescription operationDescription, BindingParameterCollection bindingParameters 
    ) 
    { return; } 

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) 
    { 
     clientOperation.ParameterInspectors.Add(new Inspector(callback)); 
    } 

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) 
    { 
     dispatchOperation.ParameterInspectors.Add(new Inspector(callback)); 
    } 

    public void Validate(OperationDescription operationDescription) { return; } 

    #endregion 

    //Parameter inspector 
    class Inspector : IParameterInspector 
    { 
     string callback; 
     public Inspector(string callback) 
     { 
      this.callback = callback; 
     } 

     public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState) 
     { 
     } 

     public object BeforeCall(string operationName, object[] inputs) 
     { 
      string methodName = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters[callback]; 
      if(methodName !=null) 
      {      
       //System.ServiceModel.OperationContext.Current.OutgoingMessageProperties["wrapper"] = inputs[0]; 
       JSONPMessageProperty property = new JSONPMessageProperty() 
       { 
        MethodName = methodName 
       }; 
       OperationContext.Current.OutgoingMessageProperties.Add(JSONPMessageProperty.Name, property); 
      } 
      return null; 
     } 
    } 

} 

ответ

0

Не уверен, почему он не мог сопоставить данные проводки с входным параметром. Однако я мог бы использовать строковый параметр для данных проводки.

[OperationContract] 
[WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
[JSONPBehavior(callback = "callback")] 
Response v1_AddCustomBodyType(string data); 
Смежные вопросы