I have a windows service bus relay application running in console application. Initially I created using console app for testing. Now I have a requirement to convert this console application to windows service. All the Azure documentation only show examples with console application.
Is there is a way to create the service bus relay application using windows service, so that in my client side I don't need to run this console application(as a command prompt).
I am trying to connect cloud application to corporate/ secured network.
Created a new MVC web application to talk to relay service. Not sure what I am missing. Is there is any changes I need to do in "MyRelayTestService" config file.
using Microsoft.ServiceBus;
using System.ServiceModel;
using System.Web.Mvc;
using WCFRelay;
namespace TestRelayApplication.Controllers
{
public class HomeController : Controller
{
static ChannelFactory<IRelayTestChannel> channelFactory;
public ActionResult Index()
{
var tcpbinding = new NetTcpRelayBinding();
channelFactory = new ChannelFactory<IRelayTestChannel>(tcpbinding, "yourServiceNamespace");
channelFactory.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior
{
TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "yourKey")
});
using (IRelayTestChannel channel = channelFactory.CreateChannel())
{
var testStr = channel.DoWork(); // error on this call
}
return View();
}
}
}
Error:
Is it possible to create azure service bus relay using Windows Service application?
Yes, We can do that using Windows Service. I do a demo for it. The following is my detail steps.
1.Create a Relay namespace using the Azure portal,we can get more info from official document . And create WCF Relay on the Azure portal.
2.First Module: WCF Service Library (WCFRelay.dll)
Definition of Service Contract
[ServiceContract]
public interface IRelayTest
{
[OperationContract]
string DoWork();
}
Implementation of Service Contract
public class RelayTest : IRelayTest
{
public string DoWork()
{
return "hello";
}
}
Implement the OnStart and OnStop for the service
public partial class MyRelayTestService : ServiceBase { ServiceHost m_svcHost = new ServiceHost(typeof(RelayTest)); public MyRelayTestService() { InitializeComponent(); }
protected override void OnStart(string[] args) { // m_svcHost?.Close(); ServiceHost sh = new ServiceHost(typeof(RelayTest)); var binding = new WebHttpRelayBinding {IsDynamic = false}; var serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public); sh.AddServiceEndpoint( typeof(IRelayTest), binding, ServiceBusEnvironment.CreateServiceUri("sb", "namespace", "path")) //tomtestrelay , testtom .Behaviors.Add( new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "Key value") } ); sh.Open(); } protected override void OnStop() { if (m_svcHost == null) return; m_svcHost.Close(); m_svcHost = null; } }
5.Adding an installer to the Service
Add following code:
public ProjectInstaller()
{
// InitializeComponent();
serviceProcessInstaller1 = new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem };
serviceInstaller1 = new ServiceInstaller
{
ServiceName = "WinServiceRelayTest",
DisplayName = "WinServiceRelayTest",
Description = "WCF Relay Service Hosted by Windows NT Service",
StartType = ServiceStartMode.Automatic //set service start automatic
};
Installers.Add(serviceProcessInstaller1);
Installers.Add(serviceInstaller1);
}
6. Install Service
Navigate to the installutil.exe in your .net folder, more details please refer to another SO thread .
Install : "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe" "c:\\yourservice.exe"
uninstall : "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe" /u "c:\\yourservice.exe"
If the service can't start as expected, please check the Event viewer to get the detail exception info. Then uninstall and reinstall again.
Please check this code, It works for me
protected override void OnStart(string[] args)
{
this.EventLog.WriteEntry("Windows Service has been Started!", EventLogEntryType.Information);
try
{
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.AutoDetect;
string serviceNamespace = "Your AzureNamespace";
string sasKey = "Your SASKey";
// Create the credentials object for the endpoint.
TransportClientEndpointBehavior sasCredential = new TransportClientEndpointBehavior();
sasCredential.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", sasKey);
// Create the service URI based on the service namespace.
Uri address = ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, "Your DynamicRelayServiceName");
// Create the service host reading the configuration.
host = new ServiceHost(typeof(YourWCFServices), address);
// Create the ServiceRegistrySettings behavior for the endpoint.
IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);
// Add the Relay credentials to all endpoints specified in configuration.
foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
{
endpoint.Behaviors.Add(serviceRegistrySettings);
endpoint.Behaviors.Add(sasCredential);
}
// Open the azure service.
host.Open();
this.EventLog.WriteEntry($"Azure bus service is on at {"Your DynamicRelayServiceName"}!", EventLogEntryType.Information);
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.