Jquery call wcf service giving 200 Parser Error - asp.net

I'm new to WCF world. i'm having a solution which contains Web App(which has JQuery Ajax call to WCF services application which is another project in the solution. After 2 days of working(cracking) i'm in a state which i able pass request and get response
but while try to show that in alert gives me 200 parser Error.
Any help will be highly appreciate
Further Ref
$.ajax({
async: true,
type: 'GET', //GET or POST or PUT or DELETE verb
url: 'http://localhost:61057/Service1.svc/GetCustomer', // Location of the service
dataType: 'jsonp', //Expected data format from server
success: function (data) {//On Successfull service call
ServiceSucceeded(data);
},
error: function () { ServiceFailed(Data); } // When Service call fails
});
Web.config
<system.serviceModel>
<services>
<service name="FIN.Services.Service1" behaviorConfiguration="DefaultBehavior">
<endpoint address="http://localhost:61057/service1.svc" binding="webHttpBinding" contract="FIN.Services.IService1" behaviorConfiguration="AjaxBehavior">
<identity>
<dns value="locahost"/>
</identity>
</endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="None">
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
service interface :
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)]
[OperationContract]
string GetCustomer();

If you need no argument, add following in your ajax call ..
data: "{}",

You are using jsonp,means you want to call cross domain service.
For cross domain service you url should be like below
http://localhost:61057/Service1.svc/GetCustomer?callback=?
and also you have to make some changes in web.config
Check out the following link for complete example
Calling cross domain service in wcf

Related

WCF Configuration for https and JSON Post

As it normally happens, I accidentally overwrote my web.config file this morning that has my WCF settings already working perfectly. As you would expect, no backup...shame shame shame on me.
That being said, I can't seem to get my configuration working again.
Here is the error that I keep getting back from IIS/ASP.NET
The message with Action '' cannot be processed at the receiver, due to
a ContractFilter mismatch at the EndpointDispatcher. This may be
because of either a contract mismatch (mismatched Actions between
sender and receiver) or a binding/security mismatch between the sender
and the receiver. Check that sender and receiver have the same
contract and the same binding (including security requirements, e.g.
Message, Transport, None).
Here is an overview of what I have setup.
My Web Config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="Binding1" maxReceivedMessageSize="10000000">
<readerQuotas maxArrayLength="10000000" />
<security mode="Transport"></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="Binding1" contract="IMyService" />
</service>
</services>
</system.serviceModel>
Here is the declaration in my Service Contract:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SSMX", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
sqlStudio_Table_Permissions SSMX(string TableName);
Sadly I just can't seem to remember what I did in my previous configuration to make this work.
Any help would be appreciated.

How to access wcf service from another computer?

I have a wcf service and i install it as windows service.
I can access this service from 192.168.2.6 machine:
"net.tcp://192.168.2.5:2025/Services/mex".
i want to access this service from another computer using static ip and port.
How can access this service ?
I tried to connect net.tcp://staticIp:port/Services/mex and i got error :
Metadata contains a reference that cannot be resolved: 'net.tcp://[staticIP]:[port]/Services/mex'.If the service is defined in the current solution, try building the solution and adding the service reference again.
(I navigate my [port] to inside port 2025)
my config:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IServices" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.2.5:2025/Services" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IServices" contract="myServices.IServices"
name="NetTcpBinding_IServices">
<!--<identity>
<dns value="localhost" />
</identity>-->
</endpoint>
</client>
<services>
<service name="F8ShadowWcfLib.Services">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="F8ShadowWcfLib.IServices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://192.168.2.5:2025/Services" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
Edit1:
I remove tag from config and i add it at runtime.
myService.ServicesClient myServ = new myService.ServicesClient();
EndpointAddress myEndpointAdd = new EndpointAddress(new Uri("net.tcp://[staticIP]:[port]/Services") ,
EndpointIdentity.CreateDnsIdentity("net.tcp://f8srv.f8.com.tr:2299/Services"));
myServ.Endpoint.Address = myEndpointAdd;
I got different error : The server has rejected the client credentials.
The problem is probably related to this part:
<identity>
<dns value="localhost" />
</identity>
This work work locally because localhost makes sense locally, but across a network it doesn't.
You can validate this identity in a number of ways, such as specifying the UPN (user principal name) of the user running the service, or an SPN (Server Principal Name) of the server running the service (although for this you'll have to register a SPN).
This article should explain it a little:
http://msdn.microsoft.com/en-us/library/ms733130.aspx
To allow seperate connection set AddressFilterMode : Any
and set your identy both service and client side.
This article about identy settings:
http://msdn.microsoft.com/en-us/library/ms733130.aspx
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Services : IServices
{
.
.
.
}

Can I use JSON in a WCF Service that uses wsHttpBinding?

I need to use wsHttpBinding binding because my service needs access to HttpContext.Current.Session. I understand from my research that this is not possible with webHttpBinding. However all of my ajax is written to use JSON and I would like it very much if I didn't have to rewrite all of it.
My service works perfectly with webHttpBinding until I need to use the session.
Or, is there a way to get webHttpBinding access to the session?
EDIT:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="LiteBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="LiteBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="LiteBehavior" name="Lite.CMS.Services.LiteService">
<endpoint behaviorConfiguration="LiteBehavior" address="" binding="webHttpBinding" contract="Lite.CMS.Services.Interfaces.ILiteService" />
</service>
</services>
</system.serviceModel>
And my contract:
[ServiceContract (SessionMode=SessionMode.Allowed)]
public interface ILiteService
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
void Item_Save(string Name);
}
And Implementation:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class LiteService : ILiteService
{
public void Item_Save(string Name)
{
// I can't get access to HttpContext.Current.Session variables here.
}
}
webHttpbinding is a stateless binding, it doesn't use SOAP.
If you try to put SessionMode=SessionMode.Required it will throw an error on service start.
If you want to implement session on a stateless protocol, you'll need to do it by hand with cookies.
See :
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/fe2a2ce9-ba97-4784-95f8-bdce5ffcd8eb/
How to Manage Sessions in Restful WCF Service
REST WCF service and Session‏ in ASP.NET

JSON WCF Service 404 on deployment server

I'm trying to deploy a WCF Service to my server, it works just the way I want it to locally. But on the server I get a 404 message.
This is what it looks like when I call my test method locally:
Image of the wanted result
When I deploy it I can still successfully browse to:
www.my domain name.com/Service1.svc
but when I go to:
www.my domain name.com/Service1.svc/test
I get a 404 error. What could be causing this?
This is all relevant code:
IService1.cs
namespace HighscoreWebService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "Test",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
string Test();
}
}
Service1.svc.cs
namespace HighscoreWebService
{
public class Service1 : IService1
{
public string Test()
{
return "Hello world!";
}
}
}
Part of Web.config
<system.serviceModel>
<services>
<service name="HighscoreWebService.Service1"
behaviorConfiguration="jsonRestDefault">
<host>
<baseAddresses>
<add baseAddress="http:/xxxxxx"/>
</baseAddresses>
</host>
<endpoint name="jsonRestEndpoint"
behaviorConfiguration="RESTFriendly"
binding="webHttpBinding"
contract="HighscoreWebService.IService1">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="jsonRestDefault">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
I suspect I made something wrong in the web.config. I'm very new to asp.net so it's possible I made any number of beginner mistakes. But because this works locally it could be something wrong with the configuration of the server I guess. Or something in the web.config I need to do to adapt to the server configuration.
Thank you for reading this.
try adding an "address" tag in your web.config endpoint. Here is an example of what I have in my WCF testing project. Though I would think that it should work how you have it. This may sound silly too, but make sure the request you are making to the service is actually an HTTP GET. Maybe post your client code too, if any of that doesnt work.
<endpoint binding="webHttpBinding" bindingConfiguration="testBinding" contract="ASMXtoWCF.IWcf"
address="test" behaviorConfiguration="RestServiceBehavior">
</endpoint>
You have to add to your merhodTest the behaviorto accept GET request (by default it accepts only POST). To do this add the WebInvoke behavior to it, for example by adding the following attribute to your method implementation (it's a behavior)
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
btw you can remove the host tag from your web config it is unecessary

Hide REST methods from SOAP WCF Client

I created a simple WCF Service interface:
namespace ApiDoc.SampleApi
{
/// <summary>
/// Contract
/// </summary>
/// <webMethodsPrefix>Web</webMethodsPrefix>
[ServiceContract(Namespace = "apidoc.sampleapi.com", Name = "SampleApi")]
public interface IService
{
[WebGet( UriTemplate = "Add?value1={value1}&value2={value2}&apiKey={apiKey}", BodyStyle = WebMessageBodyStyle.Bare)]
AddRs AddWithHttpGet(int value1, int value2, string apiKey);
[WebInvoke(Method = "POST", UriTemplate = "Add", BodyStyle = WebMessageBodyStyle.Bare)]
AddRs Add(AddRq rq);
}
}
In this case it is just a simple Add operation.
It works well for Xml, Soap and Json. Both Get and Post.
The issue I am having is in Soap when I create a Service Reference to this service. I can call both functions "Add" and "AddWithHttpGet", while I only would like to see "Add".
I originally thought it was related to using "OperationContract" attribute, but it seems like it is not used any longer. I tried adding this attribute only to POST Add, but it doesn't make any difference. I am using ASP.NET 4.0.
Another solution would be to create a different IService for Soap, but I would rather keep this all in one interface.
Here is my config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="jsonHttpBinding" />
<binding name="xmlHttpBinding" />
</webHttpBinding>
</bindings>
<services>
<service name="ApiDoc.SampleApi.Service" behaviorConfiguration="ApiDocSampleApiBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost/ApiDoc.SampleApi/" />
</baseAddresses>
</host>
<endpoint name="soap" address="" binding="basicHttpBinding" bindingNamespace="apidoc.sampleapi.com" contract="ApiDoc.SampleApi.IService" lockAttributes="bindingNamespace" />
<endpoint name="json" address="json" binding="webHttpBinding" bindingNamespace="apidoc.sampleapi.com" bindingConfiguration="jsonHttpBinding" contract="ApiDoc.SampleApi.IService" behaviorConfiguration="JsonBehavior" />
<endpoint name="xml" address="xml" binding="webHttpBinding" bindingNamespace="apidoc.sampleapi.com" bindingConfiguration="xmlHttpBinding" contract="ApiDoc.SampleApi.IService" behaviorConfiguration="XmlBehavior" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" bindingNamespace="apidoc.sampleapi.com" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
<behavior name="XmlBehavior">
<webHttp defaultOutgoingResponseFormat="Xml" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ApiDocSampleApiBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
...
I would create a separate interface.
Think of your interface like a real contract between two parties. You are using it to define what operations are available to a client. If it's in the contract, it's available.
Instead of looking for some sort "invisible ink" that would make certain parts of the contract available to certain clients, I'd simply create two contracts.
I'm a little confussed. The OperationContractAttribute is required. A WebGetAttribute on it's own shouldn't do anything, as the method is not expossed as an operation.
As for hiding an operation, that is not possable either. If the interface is your contract, and you definatly want two different contracts then you will need two different interfaces.
If you don't want to duplicate your code then you can still use inheritace. Have one interace define your SOAP operation Add and then inherit from that to add AddWithHttpGet. Then by targeting the different interfaces in your endpoints, the SOAP endpoint would have one operation and REST endpoint would have two.
try this you want hide webmethod form soap header in c#
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare), WebMethod]
public string Operation(RIL_OB_MSG RIL_OB_MSG)
{
}

Resources