Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从UWP应用程序连接到本地主机上托管的webservice_C#_Web Services_Iis_Xamarin_Xamarin.forms - Fatal编程技术网

C# 无法从UWP应用程序连接到本地主机上托管的webservice

C# 无法从UWP应用程序连接到本地主机上托管的webservice,c#,web-services,iis,xamarin,xamarin.forms,C#,Web Services,Iis,Xamarin,Xamarin.forms,我在用Xamarin表单处理web服务。我制作了一个简单的web服务,托管在IIS Express(VS 2015附带)上。Web服务处理员工信息 这在最初使用Windows Emulator时运行良好。但是我在使用Android模拟器时遇到了一些问题。在寻求帮助时,我从一个人那里得到了以下说明 按照说明操作后,不仅在android仿真器中无法工作,UWP应用程序也停止工作 以下是它失败的地方 public class RestClient<T> { pri

我在用Xamarin表单处理web服务。我制作了一个简单的web服务,托管在IIS Express(VS 2015附带)上。Web服务处理员工信息

这在最初使用Windows Emulator时运行良好。但是我在使用Android模拟器时遇到了一些问题。在寻求帮助时,我从一个人那里得到了以下说明

按照说明操作后,不仅在android仿真器中无法工作,UWP应用程序也停止工作

以下是它失败的地方

public class RestClient<T>
    {
        private const string WebServiceUrl = "http://localhost:52792/api/Employees/";
        //private const string WebServiceUrl = "http://services.groupkt.com/country/get/all/";

        public async Task<List<T>> GetAsync()
        {
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.ExpectContinue = false;
            var json = "";
            try
            {

               **json = await httpClient.GetStringAsync(WebServiceUrl);
            }
            catch (HttpRequestException e)
            {
                var error = "";
                error=e.StackTrace;
                error = e.Source;
                error = e.HelpLink;
                error = e.ToString();


            }

            var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

            return taskModels;
        }
公共类RestClient
{
私有常量字符串WebServiceUrl=”http://localhost:52792/api/Employees/";
//私有常量字符串WebServiceUrl=”http://services.groupkt.com/country/get/all/";
公共异步任务GetAsync()
{
var httpClient=新的httpClient();
httpClient.DefaultRequestHeaders.ExpectContinue=false;
var json=“”;
尝试
{
**json=await httpClient.GetStringAsync(WebServiceUrl);
}
捕获(HttpRequestException e)
{
var误差=”;
错误=e.StackTrace;
误差=e.源;
错误=e.HelpLink;
错误=e.ToString();
}
var taskModels=JsonConvert.DeserializeObject(json);
返回任务模型;
}
下面是抛出的错误

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Runtime.InteropServices.COMException: The text associated with this error code could not be found.

A connection with the server could not be established

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpHandlerToFilter.<SendAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpClientHandler.<SendAsync>d__86.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<GetContentAsync>d__32`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Plugin.RestClient.RestClient`1.<GetAsync>d__1.MoveNext()
System.Net.Http.HttpRequestException:发送请求时出错。-->System.Runtime.InteropServices.comeexception:找不到与此错误代码关联的文本。
无法建立与服务器的连接
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Net.Http.HttpHandlerToFilter.d_u4.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Net.Http.HttpClientHandler.d_u86.MoveNext()上
---内部异常堆栈跟踪的结束---
在System.Net.Http.HttpClientHandler.d_u86.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Net.Http.HttpClient.d_u58.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Net.Http.HttpClient.d_u32`1.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在Plugin.RestClient.RestClient`1.d_u 1.MoveNext()处
我不知道这里出了什么问题

更新:

这在Windows和Windows Phone应用程序中仍然可以正常工作
UWP应用程序失败。

在RestClient类中,URL
http://localhost:52792/api/Employees/
指向设备的本地主机,而不是承载IIS Express的计算机。

如果您的UWP应用程序正在尝试通过本地主机访问服务,请确认它具有环回豁免

有关更多详细信息,请参阅。特别注意,一些人发现环回豁免表可能会损坏,清除所有豁免并重新应用任何要求似乎可以解决此问题


从您发布的内容来看,似乎您在每一步都有不同的url和路径。因此,如果没有准确的信息,很难判断什么时候出错。看起来您的客户端可能根本没有正确的WebServiceUrl。您最终解决了问题吗?我们如何与localhost联系?即主要的本地主机主机不是模拟器localhost