无法使用参数调用WebInvoke方法

时间:2014-07-30 13:08:59

标签: c# wcf wcf-configuration webinvoke

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

在IIS上实现接口并托管WCF服务后,我可以从客户端调用上面的OperationContract。

但是当为它添加一个参数时,我可以调用v1_GetCustomBodyType,但是在调用v1_AddCustomBodyType时遇到以下错误 &#34;底层连接已关闭:接收&#34;

发生意外错误
[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;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

不确定为何无法将发布数据映射到输入参数。但是,我可以使用字符串参数作为发布数据。

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