Host Service to receive notifications from Web Service in UWP

139 Views Asked by At

I'll explain my use case that I'm trying to solve which will give you clear picture of what I need?

We have a web service hosted on a device and I'm able to consume the API's exposed by the service through the wsdl files that they share. But for some features we need to host a service that implements one particular interface from their exposed wsdl.

I have to host a service and send the URL in the one of the subscription call API to the service.

If I host a service with a class implementing that contract then the web service on the device sends notifications to my self hosted service and i can catch the response.

Now I couldn't host a service with a class object that implements that contract, those all can be done using ServiceHost class from "System.ServiceModel" which is not available in UWP.

I need alternative to ServiceHost to achieve the above use case.

One approach that I tried is adding a .NET standard project and a Win Runtime Project under same UWP app solution.

I added Runtime component proj reference in UWP project and .NET standard project reference in Win Runtime Component Proj.

I can now target the .NET standard project to target .net framework as well by changing the .csproj file and now could access ServiceHost, but at runtime

I get System.TypeLoadException for including ServiceHost indirectly.

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'System.ServiceModel.ServiceHost' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Source=ClassLibrary StackTrace: at ClassLibrary.Class1.HostLocalService() in C:\Users\ssiddhardha\source\repos\App3\ClassLibrary\Class1.cs:line 15 at RuntimeComponent1.Class1.WrapperHost() in C:\Users\ssiddhardha\source\repos\App3\RuntimeComponent1\Class1.cs:line 15 at App3.MainPage.OnNavigatedTo(NavigationEventArgs e) in C:\Users\ssiddhardha\source\repos\App3\App3\MainPage.xaml.cs:line 34

1

There are 1 best solutions below

1
Xie Steven On

The ServiceHost class is not available in .Net Standard 2.0. You could see: https://learn.microsoft.com/en-us/dotnet/api/?view=netstandard-2.0&term=System.ServiceModel.ServiceHost

So, you could not use it in your UWP project. If you have to use this API, you could think about using Brokered Windows Runtime Components for a side-loaded UWP app.