Reputation: 2914
Requirement:
Need to deploy REST API on Hosting Server. So migrated the code from C# Console Application to ASP.NET which was running successfully on localhost with some errors
Here is the below code
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);
}
}
}
}
Actual Result:
On making/sending the POST Request via PostMan client software, computed values were displayed in addition to that error which was shown on Output window from Debug was
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)
After Refreshing the page, an exception was raised by the following codes
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.
Without the Catch Block, the below exception was getting stacked in the Diagnostic Tools window from the Events tab
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.")
How would I need to catch these exceptions
Changing the string baseAddress = "http://somedomain.xy:8081/Calculator";
and uploading it to the server in the
PostMan it was displaying as
Could not get any response There was an error connecting to http://somedomain.xy:8081/Calculator/sum.
Expected Result:
Making calls via PostMan the computed values should need to be displayed
Since I needed to make the service up and running on hosting server. As I'm neither able to figure out the root cause nor the solutions for the whole problem, whether it is popping out from the coding part or the problem with the port?
Upvotes: 3
Views: 1118
Reputation: 2914
As said by Crowcoder & Josh Darnell, we need to create WCF service by then and can be hosted on any domain without causing problems around the port
Here in my Git repo, you can find entire Calculator code attached with publishing on to the server also. GitHub Repo
You can disable the telemetry service in the command prompt by
set DOTNET_CLI_TELEMETRY_OPTOUT=1
Upvotes: 0
Reputation: 11433
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)
These lines in the Output window are all totally normal and can be ignored (threads exiting with non-error code 0 when they are no longer in use, telemetry logging, assemblies being loaded into the IIS process).
The specific error your're getting afterwards is because you're trying to host the WCF service multiple times. This is why it works the first time you load the page, but fails after refreshing the page (because you've reconfigured and re-started another instance of the service).
While your code would work in a Console application, this is not how you host a WCF service in IIS. There's a detailed walkthrough of that setup here on the Microsoft Docs site:
Deploying an Internet Information Services-Hosted WCF Service
While the full tutorial is not really appropriate for a Stack Overflow answer, I will quote the general steps:
Ensure that IIS, ASP.NET, WCF, and the WCF activation component are correctly installed and registered.
Create a new IIS application, or reuse an existing ASP.NET application.
Create a .svc file for the WCF service.
Deploy the service implementation to the IIS application.
Configure the WCF service.
Upvotes: 1