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

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

但是当为它添加如下参数时,我可以调用 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;
        }
    }

}
4

1 回答 1

0

不知道为什么它不能将发布数据映射到输入参数。但是,我可以将字符串参数用于发布数据。

[OperationContract]
[WebInvoke(UriTemplate = "/v1/addCustomBodyType", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[JSONPBehavior(callback = "callback")]
Response v1_AddCustomBodyType(string data);
于 2014-07-31T02:59:38.163 回答