我有一个 MVC 应用程序,它依赖于 Web API 应用程序,我将这两个应用程序托管在共享托管环境上。子域上的 API 和主域上的 MVC API 是 api.mydomain.com,MVC 是 mydomain.com,每当我在邮递员或浏览器上尝试时,API 都可以正常工作,但 MVC 无法连接到它,并出现以下错误。
由于目标机器主动连接,无法建立连接
拒绝它 127.0.0.1:64527 描述:未处理的异常
发生在当前 Web 请求执行期间。请
查看堆栈跟踪以获取有关错误及其位置的更多信息
它起源于代码。
异常详细信息:System.Net.Sockets.SocketException:无连接
可能是因为目标机主动拒绝
它127.0.0.1:64527
来源错误:
执行期间产生未处理的异常
当前的网络请求。有关原产地和地点的信息
可以使用异常堆栈跟踪来识别异常
以下。
堆栈跟踪:
[SocketException (0x274d): 无法建立连接,因为
目标机主动拒绝127.0.0.1:64527]
System.Net.Sockets.Socket.InternalEndConnect(IAsyncResult
异步结果)+6995036
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)+84
System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败,
套接字 s4、套接字 s6、套接字&套接字、IP地址&地址、
ConnectSocketState 状态、IAsyncResult asyncResult、Exception&
例外)+256
[WebException:无法连接到远程服务器]
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
+606 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult
ar) +64
[HttpRequestException:发送请求时发生错误。]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务
任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)+58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
+28 SMMClient.<Setting>d__119
1.C:\Users\Dload\documents\visual studio 中的MoveNext()
2017\项目\SMM\SMMClient\Proc.cs:1369
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务
任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)+58 System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
+28 SIMSClient.Controllers.<Index>d__0.MoveNext() in C:\Users\Dload\documents\visual studio
2017\Projects\SMM\SMMClient\Controllers\HomeController.cs:19
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +58
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult
asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult
asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult
异步结果)+10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
asyncResult) +34
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
+50 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
+228 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult
异步结果)+10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult
asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
+26 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult
asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult
异步结果)+10
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult
asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult
asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
异步结果)+29
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
+38 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
异步结果)+22
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +48
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +28
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult
asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult
asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult
异步结果)+29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +48
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
+28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult
结果)+9
System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar)
+152 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult
ar) +125
这就是我从 Global.asax.cs 连接到 API 的方式
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Proc.Configure("https://api.mydomain.com/");
}
}
这就是我所说的资源
public static class Proc
{
private static HttpClient _client;
public static void Configure(string Baseurl)
{
_client = new HttpClient();
_client.BaseAddress = new Uri(Baseurl);
_client.DefaultRequestHeaders.Clear();
//Define request data format
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public static async Task<T> Settings<T>()
{
if (!string.IsNullOrWhiteSpace(Account.AccessToken))
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Account.AccessToken);
var resp = await _client.GetAsync($"api/superadmin/settings");
var txt = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(txt);
}
}
我可以从本地 MVC 应用程序连接到实时 API,但无法从实时 MVC 应用程序连接到 API。
我会很高兴得到一些帮助。