.Net Core、WCF、およびODATAクライアント

1Cでのこれらの技術の使用に関する記事を書きたかったのですが、今は.Net CoreでWCF、ODATAの使用について書くことにしました。 したがって、この記事はすべてのRuslishとKommersantにとってそれほど面倒ではありません。 .Net Coreテクノロジーは若くてペースが速いです。 さらに、それはその兄とは異なります。



.Net CoreのアプリケーションのVS 2015 Update 3には、「サービスリンクの追加」というメニュー項目はありません。 代わりに、接続サービスの追加があります。 インストール方法については、 WCF Connected Service for .NET Core 1.0およびASP.NET Core 1.0 is availableをご覧ください 。 実験サービスでは、Webサービスの中央銀行のWebサービスが、毎日のデータ(為替レート、貴金属の割引価格など)を受信するために選択されました 。 一般的に、上記のトレーニングマニュアルで接続しました。 クラスの説明を得た。 しかし、兄とは違いがあります。



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





まず第一に、彼らは設定ファイルを残しました。



リスト:



 public enum EndpointConfiguration { DailyInfoSoap, DailyInfoSoap12, }
      
      





そしてコンストラクター



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





そしてヘルパーメソッド:



 //  ,      .  . 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)); }
      
      





つまり、すべてがコードで記述されています。



.Net Coreの2番目の機能は、現在DataSetがないことです。 System.Data.DataTableは単なる空の宣言です。 したがって、大規模な.NetでSystem.Data.DataSetを返すメソッドは、Coreの.Netバージョンで、ArrayOfXElementのわかりにくい説明の標準を返します。 たとえば、次のメソッド:



 var dataset = await client.EnumValutesAsync(false);
      
      





DataSetを使用したスキームがあるため。 ドキュメントの検証中にXMLスキーマ情報にアクセスしたことを思い出し、ExpandoObjectで回路図の読み取りを行いたいと考えました。 しかし、.Net CoreにはXmlSchemaがまだありません。



それでは、ODATAに進みましょう。 このテクノロジーはWCFに似ています。 OData Client Code Generation Toolを使用します 。 例として、ODATA V4バージョンでサービス利用します。



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





ODATAを使用していない人は、ここで例を見ることができます-.NETクライアント(C#)からODataサービスを呼び出す この場合も、.Net CoreでODATAを使用するときに機能があります。



だから無害な方法:



 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); }
      
      





エラーが発生します。



.NET Frameworkの必須バージョンでは、データサービスの要求時に直接列挙することはできません。 これは、データサービスに転送するときに、同期要求が自動的に送信されるためです。 このバージョンの.NET Frameworkは非同期操作のみをサポートしているため、代わりにBeginExecuteメソッドとEndExecuteメソッドを呼び出して、列挙をサポートするクエリの結果を取得する必要があります。


つまり、次のコードが必要です。



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





しかし、これは私たちのアプローチではありません。 OData非同期拡張機能をDataServiceCollectionメソッド拡張するという記事がインターネットで見つかりました この記事のソースは ODataAsyncExtensions.csです。



メソッドがあります:



 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; }
      
      





しかし実際には、DataServiceQueryのMicrosoft.OData.Client側に通常のメソッドがあります。



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





したがって、それを使用します。



 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.github.io/odata.net/#OData-Client-Code-Generation-Tool var result = await query.ExecuteAsync(); foreach (var product in result) { DisplayProduct(product); }
      
      





大規模な.NetでのODATA 1Cサービスの使用については、 Linq to ODATAの記事を参照してください 。 .Net Coreは急速に発展しています。



記事の終わりに、アドバイスをお願いします。 次の理由により、1CでのWCFおよびODATAの使用については書いていません。



1. 1CにはWCFに類似したものがありますが、WSプロトコル、ヘッダーのメッセージを理解していません(すべてがそこに変更されています)。 しかし、彼らとのサービスは見つかりませんでした。

2. C ODATAは、.Netから作業するのに非常に便利です。 もちろん、.Netのメソッドで個別のアセンブリを作成することも、動的コンパイルを使用することもできますが、ほとんどの場合、1C kiはC#を知りません。



現時点では、1Cのニックネームはそのようなトピックに興味を持っています。



1Cユーザー、Webページ、Skypeのモバイルアプリケーション、WhatsApp間でメッセージ、ファイル、データ交換を送信するための1C Messenger



1Cの.Net。 HTTPClient、AngleSharpの例を使用します。 CSSセレクターを使用したJala承認を含むAngleSharpライブラリーを使用したサイトの便利な解析。 動的コンパイル



初心者向けの1Cでの.Netクラスの使用



だから誰かがすごい効果を持つコンポーネントを持っているなら、書いてください、私は非常に感謝します。



All Articles