wcf - 从.net core调用Service Fabric

标签 wcf azure .net-core azure-service-fabric

正在探索 Service Fabric Microservices Azure 的功能并且非常喜欢它。我在那里托管了一些简单的 WCF 服务,我可以使用 WcfClientCommunicationFactory 类从 .NET Framework 客户端调用这些服务。一切正常。

现在我想从 .NET Core 控制台应用程序调用我的微服务。这些关键字的搜索结果倾向于使用 .NET Core 来实现实际服务,但我想为客户端使用 .NET Core。

.NET Core 应用程序可以与 Service Fabric 微服务通信吗?如果是,那么 WcfClientCommunicationFactory 的 .NET Core 等效项是什么?

最佳答案

工厂的正确名称是 WcfCommunicationClientFactory,位于 Microsoft.ServiceFabric.Services.Communication.Wcf.Client来自 Microsoft.ServiceFabric.Services.Wcf NuGet package 的命名空间。但目前它与 .Net Core 不兼容,因为整个 WCF 方法和 Service Fabric 都不兼容。

Service Fabric 微服务最终是一种 REST 服务,因此您可以通过三种方法来托管它 ( from MSDN ):

  • No specific protocol: If you don't have a particular choice of communication framework, but you want to get something up and running quickly, then the ideal option for you is service remoting, which allows strongly-typed remote procedure calls for Reliable Services and Reliable Actors. This is the easiest and fastest way to get started with service communication. Service remoting handles resolution of service addresses, connection, retry, and error handling. This is available for both C# and Java applications.
  • HTTP: For language-agnostic communication, HTTP provides an industry-standard choice with tools and HTTP servers available in many different languages, all supported by Service Fabric. Services can use any HTTP stack available, including ASP.NET Web API for C# applications. Clients written in C# can leverage the ICommunicationClient and ServicePartitionClient classes, whereas for Java, use the CommunicationClient and FabricServicePartitionClient classes, for service resolution, HTTP connections, and retry loops.
  • WCF: If you have existing code that uses WCF as your communication framework, then you can use the WcfCommunicationListener for the server side and WcfCommunicationClient and ServicePartitionClient classes for the client. This however is only available for C# applications on Windows based clusters. For more details, see this article about WCF-based implementation of the communication stack.

注意:您需要ASP.NET Core tools for Visual Studio 2017 。 Visual Studio 2015 的 .NET Core 工具不再更新。

注意事项 #2:虽然 ASP.NET Core 应用可以在 .NET Core 或完整的 .NET Framework 上运行,但 Service Fabric 服务目前只能在完整的 .NET Framework 上运行。这意味着当您构建 ASP.NET Core Service Fabric 服务时,您仍然必须以完整的 .NET Framework 为目标。所以 NuGet packages from Service Fabric 都没有面向完整的 .NET Framework。

ASP.NET Core 应用程序可以作为 guest 可执行文件托管在 Service Fabric 上,无需更改代码,但建议的方法是托管 ASP.NET Core in a Reliable ServiceKestrel ( NuGet package ) 或 WebListener ( NuGet package ):

enter image description here

在 Service Fabric 中发布服务器后,您可以从任何地方使用 HTTP 协议(protocol)连接到它 - Web Frontend , Angular SPA , Xamarin或简单的 C# HttpClient ,因此客户端不需要定位到完整的 .NET Framework,但服务器可以。

一些示例代码:

static HttpClient client = new HttpClient();

client.BaseAddress = new Uri(MICRO_SERVICE_URI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
    // deserialize the result from JSON
    var result = await response.Content.ReadAsAsync<DtoClass>();
}

关于wcf - 从.net core调用Service Fabric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238974/

相关文章:

c# - "The creator of this fault did not specify a Reason"异常

c# - 在 IIS 上回收 WCF Web 服务

wcf - WCF 服务中的自定义客户端证书和用户名验证

azure - 如何从本地和azure中从terraform执行azure中的PowerShell文件

.net-core - 如何在 Blazor Server 应用程序中停止事件传播

ASP.NET 5 DNX : How to shutdown application properly?

c# - wcf中的文件上传rest : special characters in stream gets replaced when reading

azure - $图像= "mystaticsite:v1"bash : =: command not found azure cloud shell bash

容器应用服务中缺少 Azure 托管服务标识终结点

.net-core - 在使用Http.sys和URLPrefix时,如何配置dotnet core 3来提供React SPA?