要求:
需要在Hosting Server上部署REST API。因此,将代码从C#控制台应用程序迁移到了ASP.NET,该ASP.NET在localhost上成功运行,但出现了一些错误
这是下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Net;
using System.Diagnostics;
namespace WebAPI
{
[ServiceContract]
public interface ICalculator
{
[OperationContract, WebInvoke(UriTemplate = "/sum", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
double sum(double x, double y);
}
public class Calculator : ICalculator
{
public double sum(double x, double y)
{
return x + y;
}
}
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Debug.WriteLine("Initializing URL");
string baseAddress = "http://localhost:8081/Calculator";
WebServiceHost myHost = new WebServiceHost(typeof(Calculator), new Uri(baseAddress));
try
{
System.Diagnostics.Debug.WriteLine("Starting Service ...");
myHost.Open();
}
catch (Exception ex)
{
//Response.Write(ex);
//System.Diagnostics.Debug.WriteLine("Exception");
//myHost.Close();
//throw new FaultException(ex.Message);
}
}
}
}
实际结果:
通过PostMan客户端软件发出/发送POST请求时,除了显示错误外,还显示了计算值,该错误显示在“调试”窗口的“输出”窗口中。
The thread 0x3104 has exited with code 0 (0x0).
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2019-03-22T07:52:36.2582149Z","tags":{"ai.internal.sdkVersion":"web: 2.0.0.25000","ai.device.roleInstance":"DESKTOP-5DRIFMH","ai.operation.name":"GET /default.aspx","ai.operation.id":"mHfOluFrMKQ="},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"mHfOluFrMKQ=","name":"GET /default.aspx","startTime":"2019-03-22T13:22:36.2582149+05:30","duration":"00:00:04.1154559","success":true,"responseCode":"200","url":"http://localhost:29989/default.aspx","httpMethod":"GET","properties":{"DeveloperMode":"true"}}}}
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131977147547841156): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x332c has exited with code 0 (0x0)
刷新页面后,以下代码引发了异常
myHost.Close();
CommunicationObjectFaultedException was unhandled by user code
An exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: The communication object, System.ServiceModel.Web.WebServiceHost, cannot be used for communication because it is in the Faulted state.
Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.CommunicationObjectFaultedException' in System.ServiceModel.dll
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131977214492956233): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
throw new FaultException(ex.Message);
CommunicationObjectFaultedException was unhandled by user code
An exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: The communication object, System.ServiceModel.Web.WebServiceHost, cannot be used for communication because it is in the Faulted state.
Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll
Exception
Exception thrown: 'System.ServiceModel.CommunicationObjectFaultedException' in System.ServiceModel.dll
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131977154884215275): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
在没有捕获块的情况下,以下异常从“事件”选项卡堆积在“诊断工具”窗口中
Exception: Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll ("The ChannelDispatcher at 'http://localhost:8081/Calculator' with contract(s) '"ICalculator"' is unable to open its IChannelListener."). Exception thrown: 'System.InvalidOperationException' in System.ServiceModel.dll ("The ChannelDispatcher at 'http://localhost:8081/Calculator' with contract(s) '"ICalculator"' is unable to open its IChannelListener.")
我将如何捕获这些异常
更改string baseAddress = "http://somedomain.xy:8081/Calculator";
并将其上传到服务器中
显示为
无法获得任何响应连接到时发生错误 http://somedomain.xy:8081/Calculator/sum。
预期结果:
通过PostMan进行调用时,应该显示计算值
由于我需要使服务启动并在托管服务器上运行。由于我既无法找出根本原因,也无法解决整个问题,因此是从编码部分弹出还是从端口出现问题?
答案 0 :(得分:1)
The thread 0x3104 has exited with code 0 (0x0).
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2019-03-22T07:52:36.2582149Z","tags":{"ai.internal.sdkVersion":"web: 2.0.0.25000","ai.device.roleInstance":"DESKTOP-5DRIFMH","ai.operation.name":"GET /default.aspx","ai.operation.id":"mHfOluFrMKQ="},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"mHfOluFrMKQ=","name":"GET /default.aspx","startTime":"2019-03-22T13:22:36.2582149+05:30","duration":"00:00:04.1154559","success":true,"responseCode":"200","url":"http://localhost:29989/default.aspx","httpMethod":"GET","properties":{"DeveloperMode":"true"}}}}
'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/2/ROOT-1-131977147547841156): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x332c has exited with code 0 (0x0)
“输出”窗口中的这些行都是完全正常的,可以忽略(不再使用的线程退出,不再使用错误代码0,遥测日志记录,程序集加载到IIS进程中。)
此后出现的特定错误是因为您试图多次托管WCF服务。这就是为什么它在您第一次加载页面时有效,但是在刷新页面后失败的原因(因为您已经重新配置并重新启动了该服务的另一个实例)。
尽管您的代码可以在控制台应用程序中运行,但是这不是在IIS中托管WCF服务的方式。在Microsoft Docs网站上,有此设置的详细演练:
Deploying an Internet Information Services-Hosted WCF Service
虽然完整的教程并不真正适合于Stack Overflow的答案,但我将引用一般步骤:
确保正确安装并注册了IIS,ASP.NET,WCF和WCF激活组件。
创建一个新的IIS应用程序,或重用现有的ASP.NET应用程序。
为WCF服务创建一个.svc文件。
将服务实现部署到IIS应用程序。
配置WCF服务。
答案 1 :(得分:0)
正如Crowcoder和Josh Darnell所说,到那时我们需要创建WCF服务,并且可以将其托管在任何域上而不会引起端口问题
在我的Git存储库中,您还可以找到附带在服务器上发布的完整计算器代码。 GitHub Repo
您可以在命令提示符下通过以下方式禁用遥测服务
set DOTNET_CLI_TELEMETRY_OPTOUT=1