📜 ⬆️ ⬇️

.Net Core, WCF and ODATA clients

I wanted to write an article about the use of these technologies in 1C, but then I decided to write for the time being about using WCF, ODATA in .Net Core. Therefore, this article will not be so annoying to all Ruslisch and Kommersant. .Net Core technology is young and fast growing. At the same time, it is different from its older brother.

In VS 2015 Update 3, there is no “Add Link to Service” menu item for the application for .Net Core. Instead, there is “Add Connected Services”. How to install it, you can see here WCF Connected Service for .NET Core 1.0 and ASP.NET Core 1.0 is now available . For the experimental service, the Web service of the Central Bank was chosen. Web service for obtaining daily data (exchange rates, discount prices of precious metals ...) . In general, I connected by the manual above. Got a description of the classes. But at the same time there is a difference from the big brother.

var client = new DailyInfoSoapClient(DailyInfoSoapClient.EndpointConfiguration.DailyInfoSoap); 

First of all, they left configuration files.

So listing:
')
 public enum EndpointConfiguration { DailyInfoSoap, DailyInfoSoap12, } 

And Designer

 public DailyInfoSoapClient(EndpointConfiguration endpointConfiguration) : base(DailyInfoSoapClient.GetBindingForEndpoint(endpointConfiguration), DailyInfoSoapClient.GetEndpointAddress(endpointConfiguration)) { this.Endpoint.Name = endpointConfiguration.ToString(); ConfigureEndpoint(this.Endpoint, this.ClientCredentials); } 

And helper methods:

 //  ,      .  . static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials); private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.DailyInfoSoap)) { System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding(); result.MaxBufferSize = int.MaxValue; result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max; result.MaxReceivedMessageSize = int.MaxValue; result.AllowCookies = true; return result; } if ((endpointConfiguration == EndpointConfiguration.DailyInfoSoap12)) { System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding(); System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement(); textBindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None); result.Elements.Add(textBindingElement); System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement(); httpBindingElement.AllowCookies = true; httpBindingElement.MaxBufferSize = int.MaxValue; httpBindingElement.MaxReceivedMessageSize = int.MaxValue; result.Elements.Add(httpBindingElement); return result; } throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration)); } private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration) { if ((endpointConfiguration == EndpointConfiguration.DailyInfoSoap)) { return new System.ServiceModel.EndpointAddress("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx"); } if ((endpointConfiguration == EndpointConfiguration.DailyInfoSoap12)) { return new System.ServiceModel.EndpointAddress("http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx"); } throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration)); } 

That is, everything is written in the code.

The second feature of .Net Core is that it does not currently have a DataSet. And System.Data.DataTable is just an empty ad. Therefore, the method, which in large .Net returns System.Data.DataSet, in the .Net version of Core returns the standard for an incomprehensible description of ArrayOfXElement. For example this method:

 var dataset = await client.EnumValutesAsync(false); 

As with DataSet there is a circuit. I remembered the Accessing XML Schema Information During Document Validation and wanted to read the schema in ExpandoObject. But in .Net Core there is no XmlSchema yet.

We now turn to ODATA. The technology is similar to WCF. Use the OData Client Code Generation Tool . For example, take a service with a version of ODATA V4.

 var uri = new Uri(@"http://services.odata.org/V4/OData/OData.svc/"); var client = new ODataDemo.DemoService(uri); client.Format.UseJson(); 

Who has not worked with ODATA can see examples here - Calling an OData Service From a .NET Client (C #) . Again, there are features when using ODATA in .Net Core.

So harmless method:

 var products = client.Products.Expand(p => p.Supplier); foreach (var p in products) { Console.WriteLine("{0}\t{1}\t{2}", p.Name, p.Price, p.Supplier.Name); } 

causes an error.

The required version of the .NET Framework does not allow direct data transfer at the request of the data transmission service. This is due to the fact that when you transfer to the data service, a synchronous request is automatically sent. Since this version of the .NET Framework only supports asynchronous operations, you must instead call the BeginExecute and EndExecute methods to get the result of a query that supports enumeration.

That is, it requires the following code:

 var query = container.Products.Expand("ProductDetail,Categories"); System.AsyncCallback OnCustomersQueryComplete = (result => { foreach (var product in query.EndExecute(result)) { DisplayProduct(product); } }); query.BeginExecute(OnCustomersQueryComplete, query); 

But this is not our approach. The article Extending the OData Async Extensions to DataServiceCollection Methods was found on the Internet . This article has sources on ODataAsyncExtensions.cs .

There is a method:

 public static async Task<IEnumerable<TResult>> ExecuteAsync<TResult>(this DataServiceQuery<TResult> query) { var queryTask = Task.Factory.FromAsync<IEnumerable<TResult>>(query.BeginExecute(null, null), (queryAsyncResult) => { var results = query.EndExecute(queryAsyncResult); return results; }); return await queryTask; } 

But in fact, there is now the usual method in the Microsoft.OData.Client layout of the DataServiceQuery:

 public Task<IEnumerable<TElement>> ExecuteAsync(); 

Therefore, we use it:

 var query = container.Products.Expand("Supplier,Categories"); //https://blogs.msdn.microsoft.com/writingdata_services/2013/03/04/extending-the-odata-async-extensions-to-dataservicecollectiont-methods/ //http://odata.imtqy.com/odata.net/#OData-Client-Code-Generation-Tool var result = await query.ExecuteAsync(); foreach (var product in result) { DisplayProduct(product); } 

About using ODATA 1C services in a large .Net I have an article Linq to ODATA . So .Net Core is developing rapidly.

At the end of the article I would like to ask for advice. I did not write about using WCF and ODATA in 1C for the following reasons.

1. For WCF in 1C there is an analogue, but it does not understand the WS protocols, messages in the Header (although everything changes there). But I did not find any services with them.
2. C ODATA is very convenient to work from .Net. Of course, you can make a separate assembly with .Net methods or use dynamic compilation, but for the most part, 1C ki do not know C # for the most part.

At the moment, 1C nicks are interested in such topics.

1C Messenger for sending messages, files and data exchange between 1C users, web pages, mobile applications a la Skype, WhatsApp

.Net in 1C. On the example of using HTTPClient, AngleSharp. Convenient parsing of sites using the AngleSharp library, including authorization ala JQuery using CSS selectors. Dynamic compilation

Using .Net classes in 1C for beginners

So if anyone has components with a wow effect, write, I will be very grateful.

Source: https://habr.com/ru/post/310152/


All Articles