Consume Odata Service and get result in JSON - c#

I am consuming odata service using DataServiceContext and want to return data in json format.
I am looking something like this:
Odata Query with DataServiceContext and get result as json
If I try to add any request header in the sending request event. I can't see that header in fiddler. Although the event is firing which I have confirmed.
I came across "context.Format.usejson" and try to search it but didn't find anything which I can understand. Can somebody help me please ?
Using ODataLib to call a WCF Data Services service operation and JSON
My goal is to consume to OData Service and get result in JSON format using DataServiceContext.

Note: These steps only work if max protocol version of your service is 3 or higher. Version 3 of OData introduced a new JSON format, and the WCF Data Services client only supports this JSON format. (Old JSON payloads have things like "__metadata" at the top and "d":{...}. In the new JSON format, you'll see things like "odata.metadata", "odata.type", etc.)
First, make sure you have version 5.1 or greater of the WCF Data Sevrices client library (Visual Studio ships with an older version) and an updated version of the tooling that makes "Add Service Reference" in Visual Studio work.
You can download the latest tools installer here: http://www.microsoft.com/en-us/download/details.aspx?id=35840.
Once you've installed that, I recommend upgrading to the latest version of the WCF Data Services client by issuing the following command in the NuGet package manager console:
Install-Package Microsoft.Data.Services.Client
Once you've upgraded to the latest client library, you should be able to use JSON in the client without a problem. Right click on your project in Visual Studio, select "Add Service Reference" and enter the URL of the metadata document of the service. In v5.1 and above, this will pull down the full model of the service, which is needed to support JSON.
"Add Service Reference" will auto-generate a subclass of DataServiceContext. (You can see this generated code by selecting "Show All Files" in the solution explorer of Visual Studio and expanding the code behind the service reference.) For example, when I do "Add Service Reference" against http://services.odata.org/V3/OData/OData.svc/$metadata, the client library generates a class called DemoService. Use that derived class instead of DataServiceContext directly, and you can just call .Format.UseJson(). For example:
var context = new DemoService(new Uri("http://services.odata.org/V3/OData/OData.svc");
context.Format.UseJson();

You can call context.Format.UseJson method without providing a parameter if you load your service model inside OnContextCreated partial method as shown in the code below:
public partial class DemoService
{
partial void OnContextCreated()
{
this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
}
}

Related

How to access ServiceStack based web service from visual studio webform

I'm having trouble understanding how to consume an existing ServiceStack based web service (https://cert.web.transaction.transactionexpress.com/TransFirst.Transaction.Web/api/). I am using a standard vstudio .net 4.6 aspnet website c# form.
I tried adding the base service uri as a service reference to my existing project (Website > Add Service Reference > URI) but on building my solution I receive error: [Error: failed to generate code for the service reference. Cannot import wsdl:portType...].
I would like to think that I can interact with this service without manually building object definitions, so I must be missing a step.
Two questions:
1. Has anyone else worked with this particular service? Or can you suggest how to generate object definitions from this service?
2. Am I incorrectly assuming that generating the object definitions will give me full VStudio intellisense on my httpclient?
Since this is a ServiceStack Web Service you can use C# Add ServiceStack Reference to generate a Typed API in C# which you can use with ServiceStack's C# Service Clients.
The BaseUrl for this Service is:
https://cert.web.transaction.transactionexpress.com/TransFirst.Transaction.Web/api/
So if you install ServiceStack VS from VS.NET Extension gallery:
You can create a Typed C# API by clicking on Add ServiceStack Reference on your project:
Then you can use the Typed DTOs with ServiceStack's generic Service Clients, e.g:
var baseUrl = "https://cert.web.transaction.transactionexpress.com/TransFirst.Transaction.Web/api/";
var client = new JsonServiceClient(baseUrl);
var response = client.Post(new CreateCustomReport { ... });

How to get AssociatedWorkItem from TFS Build via Rest API in C#?

I have some questions about the new tfs rest api :-)
first question:
The new TFS Rest API interface is really nice, but how can I parse the JSON Object (e.g. /builds/ list) to a valid C# Object? Is there a Object in the Microsoft references, which I can use or do i have to string of Json?
second question:
I need the associated work items of the current TFS build (by Build ID?), it was easy to do it in the old TFS C# API. Is it possible for the new rest api as well?
You could install this Nuget package for your project and in the package. The assemblies in this package has already help you transfer the json data to the corresponding object. Check this case: What object returns from TFS2015 RestAPI
Yes. You can get the associated work items of the current TFS build with REST API: https://www.visualstudio.com/en-us/docs/integrate/api/build/builds#work-items
The REST API is as below:
POST https://{instance}/DefaultCollection/{project}/_apis/build/builds/{buildId}/workitems?api-version={version}

Magento Go C# requesting API fails to initialize

I am trying to connect to Magento API using C#. I am using Magento Go service and from what I've read I am able to use their API - I hope I am not wrong here. So here is what I did:
I added a Service Reference to http://mydomain.gostorego.com/api/v2_soap?wsdl=1, and just adding a service worked fine. Now I created a test class with GetStuff() method, which looks like this:
using ww.Feeds.MagnetoGoService;
public static string GetStuff()
{
MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient s = new MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient();
var login = s.login("username here", "key here");
return login.ToString();
}
When I run the program I get an error in first line saying:
Could not find default endpoint element that references contract 'MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Any ideas what this may be? Do I have to set something up in my Magento Go settings? Or maybe using Magento Go is not allowing API access?
Thanks a lot.
Forget SOAP with c# you will pull your hair out. Download Charls Cook's xml-rpc api libary for c# and use the xml-rpc method. You won't get all the snazzy intellisense but at least it will work. There's also c# solution from ez.newsletter they released with Cook's library demonstrating how to use 80% of the magento api calls.
Cook's library xml-rpc.net
http://www.xml-rpc.net/
ez.newsletter solution
http://code.google.com/p/csharlibformagexmlrpcapi/
If anyone ever has problems with this, my solution was this:
I used the reference in one project, but I actually called the class and had main program in another project. You need your Service reference to be in each project wherever you're using it. That fixed it! Alternatively you can create a new BasicHttpBinding() and putt all the options from app.config/web.config into that binder, then you don't need to reference to Service everywhere. I hope that helps!

consume SOAP web service

How do I consume this SOAP web service? how do I add the request header?
https://www.eway.com.au/gateway/ManagedPaymentService/test/managedCreditCardPayment.asmx?op=CreateCustomer
REF: http://www.eway.com.au/Developer/eway-api/token-payments.aspx
The easiest way is to use .NET's built-in support.
In Visual Studio, right click on your project references and 'Add Service Reference'. Give it the service URL https://www.eway.com.au/gateway/ManagedPaymentService/managedCreditCardPayment.asmx and it will generate a proxy class for you that will do all the work. You can then just e.g.
var client = new eWayServiceReference.managedCreditCardPaymentSoapClient();
client.CreateCustomer(...);
Alternatively you can generate the proxy class from a VS command prompt using svcutil.

WCF Rest Service: Tool for creating strongy typed classes via the help pages?

can anyone tell me if there is a tool for creating the strongly typed classes (available via the schema) - i suppose similar to Paste as XML type ..
But i was hoping for a tool that i could run to automatically update the classes automatically when there are changes
Does anyone know if something like this exists?
If you have a WCF Data Service (f.k.a. "Astoria" or "ADO.NET Data Service"), you can use the regular Add Service Reference in Visual Studio, point it to the SVC file for the service, and voila - you get a data service client side proxy, with strong types and all, and the ability to query your data service (a REST service) using LINQ - LINQ-to-REST, so to speak!
See the MSDN docs for a few more bits of info.

Categories

Resources