c# - Calling WCF Service from ASP.NET MVC via Ajax (CORS) -
i have wcf service run on port (25537) , have mvc application. mvc application make post call wcf service.
i got error :
options http://mylocal.com:25537/authentication request header field content-type not allowed access-control-allow-headers.
i had try many options nothing changed.
to global.asax
protected void application_beginrequest(object sender, eventargs e) { response.appendheader("access-control-allow-origin", "*"); response.appendheader("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); if (httpcontext.current.request.httpmethod == "options" ) { response.appendheader("access-control-allow-methods", "get, post"); response.appendheader("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); response.appendheader("access-control-max-age", "1728000"); httpcontext.current.response.end(); } }
to web.config
<httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="origin, x-requested-with, content-type, accept" /> </customheaders> </httpprotocol>
nothing changed.
any idea?
thanks in advance.
you should make configuration both on js-side , wcf-side
on js:
function post(type, data, timeout) { return $.ajax({ url: createurl(post, type), type: post, data: json.stringify(data), contenttype: "application/json", crossdomain: true,//<<<<-------------it`s here! processdata: false, datatype: "json", timeout: timeout }); }
app.config on wcf-side:
<system.servicemodel> <extensions> <behaviorextensions> <add name="crossoriginresourcesharingbehavior" type="nelibur.servicemodel.behaviors.enablecrossoriginresourcesharingbehavior, nelibur" /> </behaviorextensions> </extensions> ... <services> <service name="nelibur.servicemodel.services.default.jsonservicepercall"> <host> <baseaddresses> <add baseaddress="http://127.0.0.1:61111/webhost" /> <add baseaddress="https://127.0.0.1:61112/webhost" /> </baseaddresses> </host> <endpoint binding="webhttpbinding" contract="nelibur.servicemodel.contracts.ijsonservice" behaviorconfiguration="jsonbehavior" bindingconfiguration="webhttpbinding"/> </service> </services> <behaviors> <endpointbehaviors> <behavior name="jsonbehavior"> <webhttp /> <crossoriginresourcesharingbehavior /> </behavior> </endpointbehaviors> </behaviors> </system.servicemodel>
behavior class on wcf-service side:
using system; using system.collections.generic; using system.servicemodel.channels; using system.servicemodel.configuration; using system.servicemodel.description; using system.servicemodel.dispatcher; namespace nelibur.servicemodel.behaviors { /// <summary> /// provides cors support. /// </summary> public class enablecrossoriginresourcesharingbehavior : behaviorextensionelement, iendpointbehavior { /// <summary> /// gets type of behavior. /// </summary> /// <returns> /// <see cref="t:system.type" />. /// </returns> public override type behaviortype { { return typeof (enablecrossoriginresourcesharingbehavior); } } /// <summary> /// implement pass data @ runtime bindings support custom behavior. /// </summary> /// <param name="endpoint">the endpoint modify.</param> /// <param name="bindingparameters">the objects binding elements require support behavior.</param> public void addbindingparameters(serviceendpoint endpoint, bindingparametercollection bindingparameters) { } /// <summary> /// implements modification or extension of client across endpoint. /// </summary> /// <param name="endpoint">the endpoint customized.</param> /// <param name="clientruntime">the client runtime customized.</param> public void applyclientbehavior(serviceendpoint endpoint, clientruntime clientruntime) { } /// <summary> /// implements modification or extension of service across endpoint. /// </summary> /// <param name="endpoint">the endpoint exposes contract.</param> /// <param name="endpointdispatcher">the endpoint dispatcher modified or extended.</param> public void applydispatchbehavior(serviceendpoint endpoint, endpointdispatcher endpointdispatcher) { var requiredheaders = new dictionary<string, string>(); requiredheaders.add("access-control-allow-origin", "*"); requiredheaders.add("access-control-request-method", "post,get,options"); requiredheaders.add("access-control-allow-headers", "x-requested-with,content-type"); endpointdispatcher.dispatchruntime.messageinspectors.add(new customheadermessageinspector(requiredheaders)); } /// <summary> /// implement confirm endpoint meets intended criteria. /// </summary> /// <param name="endpoint">the endpoint validate.</param> public void validate(serviceendpoint endpoint) { } /// <summary> /// creates behavior extension based on current configuration settings. /// </summary> /// <returns> /// behavior extension. /// </returns> protected override object createbehavior() { return new enablecrossoriginresourcesharingbehavior(); } } }
you have include next operation in service contract:
/// <summary> /// helper method cors support. /// </summary> [operationcontract] [webinvoke(method = "options", uritemplate = "*")] void getoptions();
that`s all. hope help.
Comments
Post a Comment