Using singleton HttpClient with Microsoft.Rest.ServiceClient - getting System.MissingMethodException - c#

I am trying to rework my Azure API client to use singleton HttpClient for ServiceClient<T>, since multiple sources suggested to do that (for performance, and it is also suggested approach for HttpClient which is used behind the scenes for Microsoft.Rest.ServiceClient "in general")
I am using Microsoft.Rest.ClientRuntime.2.3.12 version
I see that Microsoft.Rest.ServiceClient has constructor for reusing HttpClient
protected ServiceClient(HttpClient httpClient, bool disposeHttpClient = true);
This is constructor for my API client in order to reuse HttpClient, but when it is called it fails with System.MissingMethodException: Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)
public MyAPIclient(ServiceClientCredentials credentials, HttpClient client)
: base(client, disposeHttpClient: false)
{
this._baseUri = new Uri("https://...");
if (credentials == null)
{
throw new ArgumentNullException("credentials");
}
this.Credentials = credentials;
Credentials?.InitializeServiceClient(this);
}
This is how I call API client
using (var apiClient = new MyAPIclient(credentials, HttpClientSingleton.Client))
//I get Method not found 'Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)'
{
//some api call
}
This is how I instantiate my http client singleton
public static class HttpClientSingleton
{
public static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
}
Why do I get this exception (I can see that ServiceClient has ctor(httpClient, bool))?
How to fix this problem?

Like Nkosi said, you should try restoring the nuget packages, clearing your obj/bin and rebuilding. This usually happens when things get out of step. With all the shadow caching etc etc things can end up mismatched. I created a small example based on your code and there weren't any issues with it. If you find that still doesn't work you should include more info on your file, like the class declarations and your using statements, and what version of .net you are using.
The below works for me for 4.5.2 ServiceClient 2.3.12
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
using (var apiClient = new MyAPIclient(null, HttpClientSingleton.Client))
//I get Method not found 'Void Microsoft.Rest.ServiceClient`1..ctor(System.Net.Http.HttpClient, Boolean)'
{
var httpClient = apiClient.HttpClient;
}
}
public class MyAPIclient : ServiceClient<MyAPIclient>
{
protected Uri _baseUri { get; set; }
protected ServiceClientCredentials Credentials { get; set; }
public MyAPIclient(ServiceClientCredentials credentials, HttpClient client) : base(client, disposeHttpClient: false)
{
this._baseUri = new Uri("https://stackoverflow.com");
this.Credentials = credentials;
if(credentials != null)
Credentials?.InitializeServiceClient(this);
}
}
public static class HttpClientSingleton
{
public static readonly HttpClient Client = new HttpClient() { Timeout = TimeSpan.FromMinutes(30) };
}
}
}

Related

How to return custom http status code CoreWCF?

I am taking over a legacy SOAP project where I need to replace the SOAP Service with a .NET Core solution. We cannot have the SOAP client change, so we cannot look at REST, GRPC, etc. I have looked at SoapCore and CoreWCF and have both working with the SOAP header username/password auth demo, however, I'm going to use CoreWCF for the time being.
The existing SOAP service uses custom http status code responses, for example, 202 is returned after the service has been processed and in some situations, where a SOAP Fault occurs. I realize this is incorrect, however, we need to maintain the existing business logic.
My questions are:
How can I configure my service to respond with a http status code 202 after the service is completed or when a certain condition is met? IsOneWay=True OperationContract will not work as this returns immediately.
How would I configure a SOAP Fault to respond with a custom http status code?
There are many old SO posts mentioning WebOperationContext, however, I cannot seem to access this within my service. OperationContext doesn't seem to have control of the HttpStatusCode. Maybe I'm missing something.
i.e.:
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
Here's my sample project breakdown:
Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;
namespace CoreWcf.Samples.Http
{
public class Program
{
public const int HTTP_PORT = 8088;
public const int HTTPS_PORT = 8443;
static void Main(string[] args)
{
IWebHost host = CreateWebHostBuilder(args).Build();
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel(options =>
{
options.ListenLocalhost(HTTP_PORT);
options.ListenLocalhost(HTTPS_PORT, listenOptions =>
{
listenOptions.UseHttps();
if (Debugger.IsAttached)
{
listenOptions.UseConnectionLogging();
}
});
})
.UseStartup<BasicHttpBindingStartup>();
}
}
Startup.cs
using CoreWCF;
using CoreWCF.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace CoreWcf.Samples.Http
{
public class BasicHttpBindingStartup
{
public void ConfigureServices(IServiceCollection services)
{
//Enable CoreWCF Services, with metadata (WSDL) support
services.AddServiceModelServices()
.AddServiceModelMetadata();
}
public void Configure(IApplicationBuilder app)
{
var wsHttpBindingWithCredential = new BasicHttpBinding(CoreWCF.Channels.BasicHttpSecurityMode.TransportWithMessageCredential);
wsHttpBindingWithCredential.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
app.UseServiceModel(builder =>
{
// Add the Demo Service
builder.AddService<DemoService>(serviceOptions =>
{
// Set a base address for all bindings to the service, and WSDL discovery
serviceOptions.BaseAddresses.Add(new Uri($"http://localhost:{Program.HTTP_PORT}/DemoService"));
serviceOptions.BaseAddresses.Add(new Uri($"https://localhost:{Program.HTTPS_PORT}/DemoService"));
})
// Add BasicHttpBinding endpoint
.AddServiceEndpoint<DemoService, IDemo>(wsHttpBindingWithCredential, "/wsHttpUserPassword", ep => { ep.Name = "AuthenticatedDemoEP"; });
builder.ConfigureServiceHostBase<DemoService>(CustomUserNamePasswordValidator.AddToHost);
// Configure WSDL to be available over http & https
var serviceMetadataBehavior = app.ApplicationServices.GetRequiredService<CoreWCF.Description.ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = serviceMetadataBehavior.HttpsGetEnabled = true;
});
}
}
}
IDemo.cs (Service Interface):
using CoreWCF;
namespace CoreWcf.Samples.Http
{
// Define a service contract.
[ServiceContract]
public interface IDemo
{
//[OperationContract(IsOneWay = true)]
[OperationContract]
string DemoRequest(string tagid, string readerid, string datetime);
}
}
Demo.cs (Service):
using CoreWCF.Channels;
using Microsoft.AspNetCore.Http;
using System.Net;
namespace CoreWcf.Samples.Http
{
public class DemoService : IDemo
{
public string DemoRequest(string tagid, string readerid, string datetime)
{
return $"Received tagid: {tagid}; readerid: {readerid}; datetime: {datetime}";
}
}
}
CustomUserNamePasswordValidator.cs:
using CoreWCF;
using System.Threading.Tasks;
namespace CoreWcf.Samples.Http
{
internal class CustomUserNamePasswordValidator : CoreWCF.IdentityModel.Selectors.UserNamePasswordValidator
{
public override ValueTask ValidateAsync(string userName, string password)
{
bool valid = userName.ToLowerInvariant().EndsWith("valid")
&& password.ToLowerInvariant().EndsWith("valid");
if (!valid)
{
throw new FaultException("Unknown Username or Incorrect Password");
}
return new ValueTask();
}
public static void AddToHost(ServiceHostBase host)
{
var srvCredentials = new CoreWCF.Description.ServiceCredentials();
srvCredentials.UserNameAuthentication.UserNamePasswordValidationMode = CoreWCF.Security.UserNamePasswordValidationMode.Custom;
srvCredentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNamePasswordValidator();
host.Description.Behaviors.Add(srvCredentials);
}
}
}
Many thanks in advance for any assistance.
cheers.
OutgoingResponse StatusCode is where you set the response code, but it is not an integer value.
If you still want to use it, try ASP.NET compatibility mode. The deployment must be in IIS.

Can not instantiate proxy of class: System.Net.HttpWebRequest. Could not find a parameterless constructor

I am upgrading my C# function app from .net 3.1 to 6.0`.
When I run my test cases, I found that, 1 of my test case failed with the below error.
Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: System.Net.HttpWebRequest. Could not find a parameterless constructor.
Basically, I am trying to mock HttpWebRequest and below is my piece of code for that.
var httpWebRequest = new Mock<HttpWebRequest>();
It is working fine in .Net 3.1. I am using Moq version 4.16.1 in both the projects.
I spent a fair bit of time when .Net 6 was initially released getting my Unit Test suite established. Here's how I do it using the same Moq version 4.16.1:
The Unit Tests get a Moq HttpClientFactory from the BaseClass:
public class UnitTests : BaseUnitTest
{
[Fact]
public async Task Should_Return_GetSomethingAsync()
{
// Arrange
IHttpClientFactory httpClientFactory = base.GetHttpClientFactory(new Uri("ExternalWebsiteUrlToMockTheResponse"), new StringContent("A Mock Response JSON Object"));
YourService yourService = new YourService(httpClientFactory);
// Act
Something something = yourService.GetSomethingAsync().Result;
// Assert
Assert.IsType<Something>(Something);
//..
}
In a BaseUnitTest.cs Class:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Moq.Protected;
public class BaseUnitTest
{
public IHttpClientFactory GetHttpClientFactory(Uri uri, StringContent content, HttpStatusCode statusCode = HttpStatusCode.OK)
{
Mock<HttpMessageHandler> httpMsgHandler = new Mock<HttpMessageHandler>();
httpMsgHandler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", new object[2]
{
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
}).ReturnsAsync(new HttpResponseMessage
{
StatusCode = statusCode,
Content = content
});
HttpClient client = new HttpClient(httpMsgHandler.Object);
client.BaseAddress = uri;
Mock<IHttpClientFactory> clientFactory = new Mock<IHttpClientFactory>();
clientFactory.Setup((IHttpClientFactory cf) => cf.CreateClient(It.IsAny<string>())).Returns(client);
return clientFactory.Object;
}
Your Service Class or Controller:
public class YourService : IYourService
{
private readonly IHttpClientFactory _clientFactory;
private readonly HttpClient _client;
public YourService(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
_client = _clientFactory.CreateClient("YourAPI");
}
public async Task<Something> GetSomethingAsync()
{
using (var request = new HttpRequestMessage(HttpMethod.Post, _client.BaseAddress))
{
request.Content = new StringContent($#"{{""jsonrpc"":""2.0"",""method"":""Something"",""params"": [""{SomethingHash}""],""id"":1}}");
using (var response = await _client.SendAsync(request))
{
//System.Diagnostics.Debug.WriteLine(response?.Content.ReadAsStringAsync()?.Result);
if (response.IsSuccessStatusCode)
{
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
var options = new JsonSerializerOptions { IncludeFields = true };
var something = await JsonSerializer.DeserializeAsync<Something>(responseStream, options);
// Check if the transactions from the address we're looking for...
if (something != null)
{
if (something.result?.from == address)
{
return something;
}
} } }
else {
string exceptionMsg = $"Message: {response.Content?.ReadAsStringAsync()?.Result}";
throw new YourGeneralException(response.StatusCode, exceptionMsg);
}
}
}
return null;
}
}
In your Program.cs
builder.Services.AddHttpClient("YourAPI", c =>
{
c.BaseAddress = new Uri("ExternalWebsiteUrlToMockTheResponse");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
c.DefaultRequestHeaders.UserAgent.TryParseAdd("Your Agent");
});
You can expand the BaseUnitTest.ccs class to have chained tests as well.
Both HttpWebRequest constructors are obsolete and should not be used. You have to use the static function "Create" to create a new instance of the HttpWebRequest class:
HttpWebRequest myReq =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com/");
To solve your issue, use the HttpClient class instead. This class has a parameterless constructor.

ASP.NET Core slow HTTP requests to Azure App Service

I've got a ASP.NET Core middleware that calls another HTTP service to check if user is authorized to proceed with request or not. At the moment it depends on a provided custom header, called X-Parameter-Id.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace ParameterAuthorization.Middleware
{
public class ParameterAuthorizationMiddleware
{
private readonly RequestDelegate _next;
private readonly IParameterAuthorizationService _parameterAuthorizationService;
public ParameterAuthorizationMiddleware(RequestDelegate next, IParameterAuthorizationService parameterAuthorizationService)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_parameterAuthorizationService = parameterAuthorizationService ?? throw new ArgumentNullException(nameof(parameterAuthorizationService));
}
public async Task InvokeAsync(HttpContext httpContext, IConfiguration configuration)
{
if (httpContext is null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (parameterRequestContext is null)
{
throw new ArgumentNullException(nameof(parameterRequestContext));
}
if (!(httpContext.Request.Headers.ContainsKey("X-Parameter-Id") && httpContext.Request.Headers.ContainsKey("Authorization")))
{
await ForbiddenResponseAsync(httpContext);
}
var parameterIdHeader = httpContext.Request.Headers["X-Parameter-Id"].ToString();
if (!int.TryParse(parameterIdHeader, out var parameterId) || parameterId < 1)
{
await ForbiddenResponseAsync(httpContext);
}
var authorizationHeader = httpContext.Request.Headers["Authorization"].ToString();
var parameterResponse = await _parameterAuthorizationService.AuthorizeUserParameterAsync(parameterId, authorizationHeader);
if (string.IsNullOrWhiteSpace(parameterResponse))
{
await ForbiddenResponseAsync(httpContext);
}
await _next.Invoke(httpContext);
}
private static async Task ForbiddenResponseAsync(HttpContext httpContext)
{
httpContext.Response.StatusCode = StatusCodes.Status403Forbidden;
await httpContext.Response.WriteAsync("Forbidden");
return;
}
}
}
And that's the HTTP call implementation:
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ParameterAuthorization.Middleware.Http
{
public class ParameterAuthorizationService : IParameterAuthorizationService
{
private readonly HttpClient _httpClient;
private readonly JsonSerializer _jsonSerializer;
public ParameterAuthorizationService(HttpClient httpClient, JsonSerializer jsonSerializer)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_jsonSerializer = jsonSerializer ?? throw new ArgumentNullException(nameof(jsonSerializer));
}
public async Task<string> AuthorizeUserParameterAsync(int parameterId, string authorizationHeader)
{
var request = CreateRequest(parameterId, authorizationHeader);
var result = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (!result.IsSuccessStatusCode)
{
return string.Empty;
}
using (var responseStream = await result.Content.ReadAsStreamAsync())
using (var streamReader = new StreamReader(responseStream))
using (var jsonTextReader = new JsonTextReader(streamReader))
{
return _jsonSerializer.Deserialize<ParameterResponse>(jsonTextReader).StringImInterestedIn;
}
}
private static HttpRequestMessage CreateRequest(int parameterId, string authorizationHead1er)
{
var parameterUri = new Uri($"parameters/{parameterId}", UriKind.Relative);
var message = new HttpRequestMessage(HttpMethod.Get, parameterUri);
message.Headers.Add("Authorization", authorizationHead1er);
return message;
}
}
}
And this is the boilerplate code to DI named HttpClient
sc.TryAddSingleton<JsonSerializer>();
sc.AddHttpClient<IParameterAuthorizationService, ParameterAuthorizationService>(client =>
{
client.BaseAddress = authorizationServiceUri;
client.DefaultRequestHeaders.Add("Accept", "application/json");
});
authorizationServiceUri is something I provide from a custom extension method.
The issue is that my calls to this service will randomly take 7, 10, even 20 seconds to this service and then it's going to be quick and then again slow. I call this exact ParameterAuthorizationService from Postman, it takes less than 50ms, constantly.
I'm attaching a screenshot from Application Insights showing the whole sequence of events.
Both services are deployed as Azure App Services under the same subscription within the same App Service Plan.
Code works just fine, but I'm already pulling my hair off having no clue what could be causing these performance abnormalities.
I've also checked TCP Connections in Azure App service and it's all green.
What could be the reason that some HTTP calls will be really slow?
Update
My App Service runs on a S1 App Service Plan. https://azure.microsoft.com/en-us/pricing/details/app-service/windows/
You should short circuit the pipeline if you write to httpContent. See Aspnet Core Middleware documentation :
Don't call next.Invoke after the response has been sent to the client.
Use something like this :
if (string.IsNullOrWhiteSpace(parameterResponse))
{
await ForbiddenResponseAsync(httpContext);
}
else
{
await _next.Invoke(httpContext);
}
Consider also handling authentification with a middleware that inherit from the aspnet core AuthenticationHandler class to leverage all the aspnet core Authentification/Authorization facilities. Here is an implementation example of a BasicAuthentification handler for simplicity.
Your ParameterAuthorizationService looks good. I don't think it's the source of your slow request call. To be sure, you can track the entire service call by measuring the time it takes and publishing-it in appinsights :
public class ParameterAuthorizationService : IParameterAuthorizationService
{
//...
private readonly TelemetryClient _telemetryClient;
public ParameterAuthorizationService(HttpClient httpClient, JsonSerializer jsonSerializer)
{
//...
_telemetryClient = new TelemetryClient();
}
public async Task<string> AuthorizeUserParameterAsync(int parameterId, string authorizationHeader)
{
var startTime = DateTime.UtcNow;
var timer = Stopwatch.StartNew();
var isSuccess = true;
try
{
return await AuthorizeUserParameterImpl(parameterId, authorizationHeader);
}
catch (Exception ex)
{
timer.Stop();
isSuccess = false;
_telemetryClient.TrackException(ex);
_telemetryClient.TrackDependency("Http", nameof(ParameterAuthorizationService), nameof(AuthorizeUserParameterAsync),
startTime, timer.Elapsed, isSuccess);
throw;
}
finally
{
if (timer.IsRunning)
timer.Stop();
if (isSuccess)
_telemetryClient.TrackDependency(
"Http", nameof(ParameterAuthorizationService), nameof(AuthorizeUserParameterAsync),
startTime, timer.Elapsed, isSuccess);
}
}
private async Task<string> AuthorizeUserParameterImpl(int parameterId, string authorizationHeader)
{
//Your original code
}
}
I believe I may have found where your trouble might be, taken from Microsoft docs:
Although HttpClient implements the IDisposable interface, it's
designed for reuse. Closed HttpClient instances leave sockets open in
the TIME_WAIT state for a short period of time. If a code path that
creates and disposes of HttpClient objects is frequently used, the app
may exhaust available sockets. HttpClientFactory was introduced in
ASP.NET Core 2.1 as a solution to this problem. It handles pooling
HTTP connections to optimize performance and reliability.
Recommendations:
Do not create and dispose of HttpClient instances directly.
Do use HttpClientFactory to retrieve HttpClient instances. For more information, see Use HttpClientFactory to implement resilient HTTP
requests.
I can see you are using HttpClient in your code, which might hint where you should focus your performance solution.
You tagged asp.net core 2.2 in the question, So I do recommend you use HttpClientFactory instead of HttpClient in your code, as suggested.

Creating proxy server with OWIN

The requirement
The organization have a proxy and it requires authentication.
A third party software supports proxy, but does not support proxy authentication.
So we want to write a small proxy program that delegates the requests from the third party software to the organization proxy.
(We use OWIN self hosting for this service. the project is a console application)
The class ProxyHandler
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
...
public class ProxyHandler : DelegatingHandler
{
public static IWebProxy Proxy { get; set; }
public ProxyHandler()
{
}
protected override async Task<HttpResponseMessage>
SendAsync(HttpRequestMessage req, CancellationToken ct)
{
var fwd = new UriBuilder(req.RequestUri);
fwd.Port = 443; //The software uses https only;
//in the test below this is not even called so does not matter
req.RequestUri = fwd.Uri;
var h = new HttpClientHandler();
h.UseCookies = true;
h.AllowAutoRedirect = true;
h.Proxy = Proxy;
var c = new HttpClient(h);
var resp = await c.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
return resp;
}
}
The class Startup
using Owin;
using System.Net.Http;
using System.Web.Http;
...
class Startup
{
public void Configuration(IAppBuilder b)
{
var cfg = new HttpConfiguration();
cfg.Routes.MapHttpRoute(
"Proxy",
"{*path}",
new {path=RouteParameter.Optional},
null,
HttpClientFactory.CreatePipeline(
new HttpClientHandler(),
new DelegatingHandler[] { }
));
b.UseWebApi(cfg);
}
}
The code that starts the proxy service and test with a download
using Microsoft.Owin.Hosting;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
...
var p = new WebProxy(proxy_uri, true)
{
Credentials = new NetworkCredential(domain_user, password)
};
ProxyHandler.Proxy = p;
var app = WebApp.Start<Startup>("http://localhost:8181/");
using (app)
{
var wc = new WebClient();
wc.Proxy = new WebProxy("localhost:8181", true);
var downloaded = await wc.DownloadStringTaskAsync
(new Uri("http://example.com/));
Console.WriteLine(downloaded);
}
The result
The call to wc.DownloadStringTaskAsync throws HTTP 400 error. The SendAsync method was not called at all.
The question
How can I make it work?

WCF: How can I reach an instance of a service from inside an instance of a service library?

I have a long-running WCF service hosted in a Windows service. I have a service library whose purpose is to report on the state of the service. How can I get to the instance of the service from inside an instance of the service library?
To illustrate, I created a service that records the time it started, and exposes a method to report how long it's been running. The service library needs to be able to call the service's ElapsedSeconds() method, so the the library needs a reference to the running service.
I thought I could use OperationContext.Current. Here's my service library class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace TimerService
{
public class TimerServiceLib : ITimerServiceLib
{
TheTimerService m_timerService;
public TimerServiceLib()
{
var currentContext = OperationContext.Current;
var instanceContext = currentContext.InstanceContext;
m_timerService = (TheTimerService)instanceContext.GetServiceInstance();
}
public double SecondsSinceStart()
{
return m_timerService.ElapsedSeconds();
}
}
}
But the call to GetServiceInstance() creates a new instance of TimerServiceLib(), which of course gives me an infinite loop. So, what is the correct way to do this?
Here is my service class, actually being hosted in a console application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace TimerService
{
public partial class TheTimerService : ServiceBase
{
private DateTime m_startTime;
ServiceHost m_svcHost;
public TheTimerService()
{
InitializeComponent();
Init();
m_startTime = DateTime.Now;
}
public double ElapsedSeconds()
{
return (DateTime.Now - m_startTime).TotalSeconds;
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
public void Init()
{
if (m_svcHost != null)
{
m_svcHost.Close();
}
string httpAddress = "http://localhost:1234/TimerService";
string tcpAddress = "net.tcp://localhost:1235/TimerService";
Uri[] adrbase = { new Uri(httpAddress), new Uri(tcpAddress) };
m_svcHost = new ServiceHost(typeof(TimerServiceLib), adrbase);
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
m_svcHost.Description.Behaviors.Add(mBehave);
var debugBehavior = m_svcHost.Description.Behaviors.Find<ServiceDebugBehavior>();
debugBehavior.IncludeExceptionDetailInFaults = true;
BasicHttpBinding httpBinding = new BasicHttpBinding();
m_svcHost.AddServiceEndpoint(typeof(ITimerServiceLib), httpBinding, httpAddress);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
NetTcpBinding tcpBinding = new NetTcpBinding();
m_svcHost.AddServiceEndpoint(typeof(ITimerServiceLib), tcpBinding, tcpAddress);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
m_svcHost.Open();
// SimShopServiceLib.m_shop = new CSimShopManager();
}
}
}
You can do it this way:
First, modify your TimerServiceLib and do the constructor injection of TheTimerService:
public class TimerServiceLib : ITimerServiceLib
{
private readonly TheTimerService m_timerService;
public TimerServiceLib(TheTimerService theTimerService)
{
m_timerService = theTimerService;
}
public double SecondsSinceStart()
{
return m_timerService.ElapsedSeconds();
}
}
Then, in your Init() upon creation of ServiceHost, instantiate first your service and pass the TheTimerService. Since your are creating the ServiceHost inside your windows service, wich is TheTimerService you can pass this.
Uri[] adrbase = { new Uri(httpAddress), new Uri(tcpAddress) };
var timerServiceLib = new TimerServiceLib(this)
m_svcHost = new ServiceHost(timerServiceLib , adrbase);
For more details about passing object in you service see this link.
Disclaimer : The above code is not tested.

Categories