从UWP App使用Windows服务

时间:2018-01-29 15:26:48

标签: uwp windows-services desktop-bridge

我正在尝试将我的Win Forms UI转换为UWP应用。现有的Win Forms应用程序与我的Windows服务进行通信。是否可以从UWP应用程序访问我的Windows服务?

2 个答案:

答案 0 :(得分:2)

您可以通过各种方式(包括套接字或RPC)从UWP应用程序与Windows服务进行通信,但您将无法在Windows应用商店中分发Windows服务。对于通过其他机制安装服务的企业解决方案,这是可以的。

对于套接字或HTTP,您需要确保UWP应用能够communicate to localhost以及network isolation is removed。对于RPC,您需要确保端点为ACLed appropriately for your UWP app's SID

答案 1 :(得分:0)

  

是否可以从UWP应用程序访问我的Windows服务?

您还可以访问您的Windows服务表单uwp。 Windows.Web.Http命名空间中的类以及相关的Windows.Web.Http.HeadersWindows.Web.Http.Filters命名空间为通用Windows平台(UWP)应用程序提供编程接口,充当HTTP client以执行基本GET请求或实现更高级的HTTP功能。

例如,您可以使用以下代码通过HTTP发送简单的GET请求。

Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();

//Add a user-agent header to the GET request. 
var headers = httpClient.DefaultRequestHeaders;

//The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
//especially if the header value is coming from user input.
string header = "ie";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

Uri requestUri = new Uri("http://www.contoso.com");

//Send the GET request asynchronously and retrieve the response as a string.
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";

try
{
    //Send the GET request
    httpResponse = await httpClient.GetAsync(requestUri);
    httpResponse.EnsureSuccessStatusCode();
    httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
    httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}

请注意,UWP应用程序具有Internet(客户端)功能声明。在使用以下代码之前,您需要检查Internet(客户端)选项。有关更多信息,请参阅App capability declarations