I created a WCF self hosted web service. Here are my serviceContract and OperationContract in Instace class:
[ServiceContract]
public interface ISwiperWS
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getstatus?callback={Callback}", ResponseFormat = WebMessageFormat.Json)]
String getStatus(String Callback);
}
TestWS.cs
public String getStatus()
{
return "true"
}
I am accessing the endpoint of this web service from JsonP written in GWT
String url = "https://somedomain.com:8083/getstatus";
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setTimeout(600);
jsonp.setCallbackParam("callback");
jsonp.send(url);
I create a setup project and installed it on different-different machine. Whenever i am making a request to web service endpoints from JsonP It is showing an strange behavior. In some machine i am getting an expected response where as in some other machine it continuously showing an error i.e. 405-method are not allowed.
I searched it for and make change according to them but nothing works for me. Please suggest me a solutoin
May be: I believe it had something to do with your jquery ajax call using jsonp. If you are going to change it to just json the post will work..
Related
I have a service on my website that works well but now I want to pass some values through url. Is this possible?
Service url: http://example.com/Service.svc
I use it like:
ChannelFactory<IService> factory = new ChannelFactory<IService>("myKeyBinding");
IService service = factory.CreateChannel();
service.Method(value);
What I want is: Service url: http://example.com/Service.svc?some=value&another=value
And use these values on my website.
WCF do support restful behaviour. It's easy:
First mark your contracts with WebInvoke
[WebInvoke(UriTemplate = "Method/{some}/{another}", Method = Get, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string Method(string some, string another);
Create a new ServiceBehavior with following metadata:
<serviceMetadata httpGetEnabled=”true”/>
Create a new endpoint based on this behavior. It should have binding as wsHttpBinding
Now, call your service like:
http://example.com/Service.svc/Method?some=value&another=value
I have two projects - a WCF service that provides operations on a database, and an ASP.NET project running AngularJS that acts as a client to the service.
I would like to combine these into a single project. That is, when running the service, the interface (the ASP.NET AngularJS project) should appear.
I have seen some sources saying that AspNetCompatibilityMode can be used to do something like this, but I haven't seen anywhere how to actually specify a client.
Is this the right way to go about doing this? Is there a simpler way? Thanks in advance!
It is possible. I assume you want to expose existing WCF service in your ASP.NET web forms/mvc (whatever) type of project.
Steps:
1) make sure your ASP.NET project references the assembly in which is the WCF service implementation
2) change in your ASP.NET project your global.asax to:
using System.ServiceModel.Activation; // from assembly System.ServiceModel.Web
protected void Application_Start(Object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.Add(new ServiceRoute("Services/Angular", new WebServiceHostFactory(), typeof(WCFNamespace.AngularService)));
}
This registers calls starting with the /Service/Angular prefix to be handled by your WCF service.
3) your WCF service should look like this
[ServiceContract]
public interface IAngularService
{
[OperationContract]
[WebGet(UriTemplate = "/Hello", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[Description("Returns hello world json object")]
HelloWorld GetHello();
}
[DataContract]
public class HelloWorld
{
[DataMember]
public string Message { get; set; }
}
Note the methods - they should be decorated with [WebGet] or [WebInvoke] methods since for Angular you want to build RESTfull wcf service. Also serialization/deserialization format is set to json.
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class AngularService : IAngularService
{
public HelloWorld GetHello()
{
return new HelloWorld { Message = "Hello from WCF. Time is: " +
DateTime.Now.ToString() };
}
}
Now you should be able to get json object if you type /Services/Angular/Hello into browser.
And finally as you've noted the WCF contract implementation (in this case class AngularService) has to be marked with attribute [AspNetCompatibilityRequirements] so the IIS can host it under ASP.NET web forms/MVC project.
Disclaimer: this is very naive implementation, in real world you probably would want to catch & log exceptions that happen in service and return them to client in form of json.
Hi all I am facing one issue with dualHttpBinding and webHttpBinding in my application. Here is the overall scenario
I have a dotNet and Android application which consumes a WCF Service (which has three different bindings).
1. basicHttpBinding - For dotNet app (Service1.svc)
2. webHttpBinding - For Android to access via Rest full service (Service2.svc)
3. dualHttpBinding - For push notification to dotNet app(PushServie.svc)
When ever android app consumes a method in Servie2(AddOrderItems) - it inserts into database and in inturn shows a message in dotNet application (a push notification). For this I have used CallBackContract in PushService and it is working fine when I try to access the AddOrderItems method (in Servie2) from dotNet app (for testing purpose I did) and notification message is coming.
But my problem is how can I come to know whether and and when my AddOrderItems is being called from android app, so that I have to show a message (via CallBackContract) in dotNet app.
I tried to call PushServie.svc method(CallBackMessagge) from Servie2.svc after calling AddOrderItems method but no luck at all.
Here is the code for Service2 and PushService.svc,
// AddOrderItems method in IService2 which is implemented in Servie2.svc
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrderItems", RequestFormat =
WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
bool AddOrderItems(RequestOrderItemData orderItemData);
and call back method in IPushService which is implemented in PushService.svc
[OperationContract(IsOneWay = true)]
void DisplayMessage(int orderID, int tableID, string tableName);
Here is the code for DisplayMessage in PushService
IPushNotificationCallBack Callback
{
get
{
return
OperationContext.Current.GetCallbackChannel<IPushNotificationCallBack>();
}
}
and the method in PushService.svc
public void DisplayMessage(int orderID, int tableID, string tableName)
{
Callback.DisplayMessage(orderID, tableID, tableName);
}
I tried to search in googles and every one says that a webHttpBindin does not support a callback functionality.
My concern is I want to call my DisplayMessage method from AddOrderItems. If this can be possible with any modifications, I am really be in happy.
As far as I think I have posted all the necessary code, if some thing else requires, please let me know, I will post it.
Any help highly appreciated.
Warm Regards,
Ganesh
I am using wsfTestClient to debug a c# wcf service program. my interface for this function has
[OperationContract]
[WebGet(UriTemplate = "AddCsv?fileLoc={fileLoc}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
void AddCsv(string fileLoc);
and the corresponding class is
public void AddCsv(string fileLoc)
{
List<Entity> listOfEntries = LoadCSV(fileLoc);
PutList(listOfEntries);
}
I have a breakpoint at the declaration of the AddCsv method and I look at the fileLoc variable and it is null. Why is this acting this way? fileLoc is a directory location.
Here is a screen shot of the wcftestclient
WCF Test Client does not work for non-SOAP endpoints (a.k.a. WCF REST services). Since you decorated your operation with [WebGet] I'm assuming this is the case for you. The issue is that non-SOAP endpoints do not expose their metadata so that tools such as WCF Test Client (or svcutil) can know how to call the service.
For more information, check the post Mixing Add Service Reference and WCF Web HTTP (a.k.a. REST) endpoint does not work.
I have a WCF Service that return JSON format Data. When i use a Web Browser i can easily see the result but when using Jquery getJSON i cannot get it to work. i can see in fiddler that it is returning the data but in firebug it shows with red font and empty response.
here is my WCF service
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetUrl/{iType}")]
String GetUrl(string iType);
public String GetUrl(string iType)
{
return strurl;
}
MY JSON call looks like this
$.getJSON("http://localhost/UrlSvc/UrlService.svc/GetUrl/1",
function (data) {
console.log("Data JSOn Got");
$.each(data.items, function (i, item) {
console.log("Data Received");
});
});
when i just put that url in browser i get this below in response as expected
{"GetChartUrlResult":"ulr_fdba9bc2-7ff7-467f-a6e0-6f4d234169d2.png"}
BUT getJSOn returns Empty Response as seen in Firebug with Red font on the url Itself.
this is a cross domain call and i have enabled cross domain it WCF
Browsers do not allow cross-domain AJAX calls and stop XMLHTTPRequest's from happening to any domain but the one that loaded the containing script or page.
To get around this, you can use JSONP calls that wrap the AJAX response in javascript. You add the the following query string entry to your request:
callback=?
Also, note that if you are using WCF, you will need to enable JSONP in your service. You do this by setting the crossDomainScriptAccessEnabled attribute to true in your to your config file:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
More information about JSONP and WCF can be found here.
If the call is cross domain, you have to use jsonp. You can use it with $.getJSON by adding ?callback=? to the URL (depending on the server-side API). Make sure that your API does support emitting the callback appropriately.
Documentation here: http://api.jquery.com/jQuery.getJSON/
Sorry to be late to the party - you DO NOT have to use jsonp, i use WCF with angularjs, jquery, mobile apps, etc - and never use jsonp.
Just decorate your class and properties like the following and it will serialize.
[DataContract]
public class MyCustomClass
{
[DataMemember]
public string Name { get; set;}
}