android调用restful wcf服务端,基于WinService,开发Restful 风格的Wcf 服务端

public classMessageInspector:IDispatchMessageInspector

{privateServiceEndpoint _serviceEndpoint;publicMessageInspector(ServiceEndpoint serviceEndpoint)

{

_serviceEndpoint=serviceEndpoint;

}///

///Called when an inbound message been received///

/// The request message.

/// The incoming channel.

/// The current service instance.

///

///The object used to correlate stateMsg.///This object is passed back in the method.///

public object AfterReceiveRequest(refMessage request,

IClientChannel channel,

InstanceContext instanceContext)

{

StateMessage stateMsg= null;

HttpRequestMessageProperty requestProperty= null;if(request.Properties.ContainsKey(HttpRequestMessageProperty.Name))

{

requestProperty=request.Properties[HttpRequestMessageProperty.Name]asHttpRequestMessageProperty;

}if (requestProperty != null)

{var origin = requestProperty.Headers["Origin"];if (!string.IsNullOrEmpty(origin))

{

stateMsg= newStateMessage();//if a cors options request (preflight) is detected,//we create our own reply message and don‘t invoke any//operation at all.

if (requestProperty.Method == "OPTIONS")

{

stateMsg.Message= Message.CreateMessage(request.Version, null);

}

request.Properties.Add("CrossOriginResourceSharingState", stateMsg);

}

}returnstateMsg;

}///

///Called after the operation has returned but before the reply message///is sent.///

/// The reply message. This value is null if the///operation is one way.

/// The correlation object returned from///the method.

public void BeforeSendReply(ref Message reply, objectcorrelationState)

{var stateMsg = correlationState asStateMessage;if (stateMsg != null)

{if (stateMsg.Message != null)

{

reply=stateMsg.Message;

}

HttpResponseMessageProperty responseProperty= null;if(reply.Properties.ContainsKey(HttpResponseMessageProperty.Name))

{

responseProperty=reply.Properties[HttpResponseMessageProperty.Name]asHttpResponseMessageProperty;

}if (responseProperty == null)

{

responseProperty= newHttpResponseMessageProperty();

reply.Properties.Add(HttpResponseMessageProperty.Name,

responseProperty);

}//Access-Control-Allow-Origin should be added for all cors responses

responseProperty.Headers.Set("Access-Control-Allow-Origin", "*");if (stateMsg.Message != null)

{//the following headers should only be added for OPTIONS requests

responseProperty.Headers.Set("Access-Control-Allow-Methods","POST, OPTIONS, GET,DELETE,PUT");

responseProperty.Headers.Set("Access-Control-Allow-Headers","Content-Type, Accept, Authorization, x-requested-with");

}

}

}

}classStateMessage

{publicMessage Message;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值