I have a WCF Data service operation :
[WebGet]
public bool isContractUpToDate(string contractId, string lastmodifiedDate);
but I don't know how to call this service from a .NET client application and how I can call this operation from Internet Explorer.
I'm looking for some examples.
We can access RESTful WCF browser services like this
http://localhost:8080/Service/isContractUpToDate/{contractId}/{lastmodifiedDate}
But I think we can't specify DateTime datatype, as per my understanding it should be string only.
I have found this series to be extremely helpful and rich with examples on how to implement WCF REST services (including query strings and filters as well as calling from client code).
I finally found an answer to my question.
to invoke the operation from the browser, I use :
http://localhost:8080/service/ctrService.svc/isContractUpToDate?contractId='1'&lastmodifieddate='2012/02/04 00:00:00'
and to do it from a .NET client, I use :
IEnumerable<bool> resp = service.Execute<bool>(new Uri("http://localhost:8080/pricingservice/PricingDataService.svc/isContractUpToDate?contractId='1'&" +"lastmodifieddate='"+DateTime.Now.ToString()+"'"));
Console.WriteLine("is contract uptodate ? " + resp.First());
This works for me:
?startDate=2014-04-11T14:45:00&endDate=2014-05-31T23:59:59
I'm using this string to send url parameters to a REST service hosted in ASP.NET application.
This is how you can call it according to MSDN
http://services.odata.org/Northwind/Northwind.svc/Customers('ALFKI')/Orders?$filter=ShippedDate gt datetime'1997-09-22T00:00:00'
datetime'1997-09-22T00:00:00
Related
I need to enable my webAPI REST service to accept a request in the format of:
www.someURL.com/OldService.svc
I am working on an existing application that used to use WCF. These methods do not have to return anything but a 200 response. We need the REST service to handle this call so we can retire the old WCF service, but systems will fail if we don't support this WCF request.
Has anybody done this before?
edit:
Is it possible to do this with just adding a new route?
You can add a WCF service (.svc) to a Web API project by simply adding a New Item and selecting Web, it will then show up in the list as WCF Service.
Maybe you could use the Route attribute(using System.Web.Http) for the old service? I've used this for route names like [Route("SomeRoute")] but I'm not 100% sure if the .svc extension will interfere with anything.
[Route("OldService.svc")]
[HttpPost]
public HttpResponseMessage NewData(Data SomeData)
{}
I'm currently using Xcelsius to connect to my WCF web service, however it doesn't work. Googling around I see that Xcelsius can't connect to WCF web services but can't seem to find out why.
Assuming the web service gives away what type it is (WCF or ASMX) is it possible to perhaps spoof this? Something similar changing your user agent in a browser.
Edit
Xcelsius is expecting the address to the services WSDL (http://localhost:3951/Service1.svc?wsdl). As for the format that my WCF service is emiting, it's
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
I've also tried using FlatWSDL, however that doesn't seem to change anything.
Simply use basicHttpBinding in your service, and will look like an ASMX service to any consumer.
That still might not be enough, if the consumer is expecting a particular format. You would then need to duplicate the expected format.
I am trying to get data form a web service inside a silverlight app. Unfortunately the silverlight app (Bing map app) just hangs when trying to connect.
I use the same code in a console app and it works just fine.
Is there anything special I need to do in silverlight to get it to work? I don't get any exceptions - it just hangs.
I based my service and client code off of this example
http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
Problems and Questions:
1. Why can't I set breakpoints in my sliverlight code?
2. How can I successfully call WCF service from a silverlight app? (links to SIMPLE working examples would be great - all the ones I seem to find seem to be quite advanced (RIA, Duplex, etc) Many of these also show xml and other non C# "code" - frankly I don't know what those do and how they relate to the projects, code and services.
(Clearly I am quite ignorant about WCF and silverlight)
As per request for code:
[ServiceContract]
public interface ILGSMapServer
{
[OperationContract]
List<double> GetLatitudes();
}
public class TreeWorkClient
{
ChannelFactory<ILGSMapServer> httpServer;
public ILGSMapServer httpProxy;
public TreeWorkClient()
{
httpServer = new ChannelFactory<ILGSMapServer>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/GetLatitudes"));
httpProxy = httpServer.CreateChannel();
}
public List<TreeWorkItem> GetLocations()
{
List<double> lats = httpProxy.GetLatitudes();
//... do stuff in code
return ret;
}
}
I agree with John Saunders - it would be easier to answer this if you published the client code.
However as a guess, a common problem with calling services from Silverlight applications is the restriction Silverlight puts on cross domain calls.
In summary, if your service is at a different domain from the site-of-origin of the Silverlight application, you need to create a client access policy file at the service location.
See this for details:
http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
Given your example code you should be seeing the
System.InvalidOperationException: The contract 'ILGSMapServer'
contains synchronous operations, which are not supported in
Silverlight. Split the operations into "Begin" and "End" parts and set
the AsyncPattern property on the OperationContractAttribute to 'true'.
Note that you do not have to make the same change on the server.
You'd need to change your service contract to the following
[ServiceContract]
public interface ILGSMapServer {
[OperationContract( AsyncPattern = true )]
IAsyncResult BeginGetLatitudes( AsyncCallback callback, object context );
List<double> EndGetLatitudes( IAsyncResult result );
}
This also means you'll need to do something completely different in your GetLocations() function as this function will return before the results from the Web have been returned.
Try taking a look at the examples here.
Other options involve using the "Add Service Reference" rather than manually defining it in code.
I believe you need to have this attribute on WCF service for SL to consume it:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
As for debugging - you can debug Silverlight, try using IE for that, its most natural browser for SL debugging (sadly).
Once you start debugging it will be more clear whats wrong when you catch cross domain exception or some other.
I have been trying fruitlessly to try and create a simple REST file upload WCF Service, which accepts more than one piece of information.
I have found a fair few places on the Internet that suggest that it is possible to have more than one parameter to a OperationContract that has a Stream as one of the parameters (Upload file with REST, Using Silverlight and WCF to create a RESTful File Upload Utility, WCF REST File Upload, Uploading File to server from ASP.Net client using WCF REST Service, etc.) but no matter how many times I try I always get the same error message.
For request in operation Upload to be a stream the operation must have
a single parameter whose type is Stream.
Is it actually possible to have an OperationContract that accepts more than one parameter when one of them is a Stream? If so are there any particular steps that need to be taken which I may have missed that would have caused for me not to be able to do so.
For reference I am using Visual Studio 2010, WCF 4.0
I have uploaded the example project that I am trying to get to work, its literally the minimum of what going by the examples I have read that I should need to be able to upload a file with additional parameters. My Example.
Yes, it is possible. I am doing it with UriTemplates.
[WebGet(UriTemplate="ReceiveChunk/{complete}?offset={offset}", Method ="POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public string ReceiveChunk(string complete, int offset, Stream fileContents)
{
//implementation
}
Is that what you're looking for?
I know this is an old question but I've been struggling with this today. I finally found this link, specifically:
2. You are not using WebEndpoint or WebServiceHost;
I was self hosting my service in a console app and was using ServiceHost not WebServiceHost. Changing the host type resolved my issue and explained my confusion. SOAP WCF services require the operation to have a single Stream parameter only, REST WCF services don't.
What is the best way to create a JSON web service? We have another team that is using Java and they insist to having all communication done using JSON. I would prefer to use WCF rather than any 3rd party framework.
I found this blog: http://www.west-wind.com/weblog/posts/164419.aspx, and it suggests that the Microsoft implementation is flawed with M$ specific crap.
If you use WCF and the 3.5 Framework, it couldn't be easier. When you mark your OperationContracts with the WebGet attribute, just set the ResponseFormat parameter to WebMessageFormat.Json. When the service is accessed RESTfully, it will return the data using the DataContractJsonSerializer.
It's really helpful to mark the POCOs that you want to JSON serialize as [DataContract] and to mark each serializable member as [DataMember]. Otherwise, you end up with funky JSON, as Rick pointed out in his blog post.
I maintain a mature Open Source alternative to WCF in ServiceStack, a modern, code-first, model-driven, WCF replacement web services framework encouraging code and remote best-practices for creating terse, DRY, high-perfomance, scalable REST web services.
It includes .NET's fastest JSON Serializer and has automatic support JSON, JSONP, CORS headers as well as form-urlencoded/multipart-formdata. The Online Demos are a good start to look at since they all use Ajax.
In addition, there's no XML config, or code-gen and your 'write-once' C# web service provides all JSON, XML, SOAP, JSV, CSV, HTML endpoints enabled out-of-the-box, automatically with hooks to plug in your own Content Types if needed.
It also includes generic sync/async service clients providing a fast, typed, client/server communication gateway end-to-end.
This is the complete example of all the code needed to create a simple web service, that is automatically without any config, registered and made available on all the web data formats on pre-defined and custom REST-ful routes:
public class Hello {
public string Name { get; set; }
}
public class HelloResponse {
public string Result { get; set; }
}
public class HelloService : IService<Hello> {
public object Execute(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
Above service can be called (without any build-steps/code-gen) in C# with the line below:
var client = new JsonServiceClient(baseUrl);
var response = client.Send<HelloResponse>(new Hello { Name = "World!" });
Console.WriteLine(response.Result); // => Hello, World
And in jQuery with:
$.getJSON('hello/World!', function(r){
alert(r.Result);
});
What is the best way to create a JSON web service? We have another
team that is using Java and they insist to having all communication
done using JSON. I would prefer to use WCF rather than any 3rd party
framework.
Here's an easy-to-follow walkthrough, which takes you through the process of setting up your first WCF Service, then linking it to a SQL Server database.
http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm
It uses Microsoft's beloved Northwind SQL Server database, and shows how to write a simple JSON WCF Web Service to read and write it's data.
Oh, and it then shows how to consume the JSON data using JavaScript or an iOS application.
Good luck !
I ended up using JayRock. Its fantastic piece of technology, just works. You don't get any NullReferenceExceptions like from this crap WCF if you don't configure it correctly.