简体   繁体   中英

Could not invoke WebInvoke method with parameter

[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();
}

After implementing the interface and hosting the WCF service on IIS, I could invoke the OperationContract above from client.

But when adding a parameter for it like following, I could invoke the v1_GetCustomBodyType, but I met the following error when invoking v1_AddCustomBodyType "The underlying connection was closed: An unexpected error occurred on a receive"

[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);
}

I call the service via WebRequest

Source code of 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;
        }
    }

}

Not sure why it could not map posting data to the input parameter. However, I could use string parameter for the posting data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM