MSMQ backed WCF service hosted in a windows service fails on startup - wcf

I have a WCF service hosted in a Windows service that I set to Automatic so it will start automatically when the server is brought up. The service is endpoint is MSMQ backed.
When I start the service manually, everything is good. But when the service starts on bootup, I get a MSMQ exception:
System.TypeInitializationException: The type initializer for
'System.ServiceModel.Channels.Msmq' threw an exception. --->
System.ServiceModel.MsmqException: The version check failed with the error:
'The Message Queuing service is not available (-1072824309, 0xc00e000b)'. The
version of MSMQ cannot be detected All operations that are on the queued channel
will fail. Ensure that MSMQ is installed and is available.
at System.ServiceModel.Channels.MsmqQueue.GetMsmqInformation
(Version& version, Boolean& activeDirectoryEnabled)
at System.ServiceModel.Channels.Msmq..cctor()
--- End of inner exception stack trace ---
It seems like the MSMQ is not ready to be used before the service starts...is there a solution to this?

You need to add a dependency on MSMQ in your WCF service host. You can do this in the service installer:
ServiceInstaller serviceInstaller = new ServiceInstaller();
// Adding this property to your ServiceInstaller forces
// your service to start after MSMQ.
serviceInstaller.ServicesDependedOn = new string[] { "MSMQ" };
If you are not using a service installer, you can also add the MSMQ dependency for your service by editing the Windows registry, as described in "Microsoft Support: How to delay loading of specific services".

Related

Installing NServiceBus as a Windows Service with dependency on RavenDb

When we install an NServiceBus 3.3.6 endpoint as a Windows service using the /install flag, it is automatically configured with a windows service dependency on Message Queuing.
However, even though our NServiceBus endpoints use RavenDb for persistence, the installer does not configure a service dependency on RavenDb. This means that when our server restarts most of our NServiceBus endpoints fail to start up due to the following exception:
System.InvalidOperationException:
The database {name} is currently being loaded, but after 30 seconds,
this request has been aborted. Please try again later, database loading continues.
Is there any way to tell NServiceBus to set up a dependency on RavenDb or is this something we have to configure manually, perhaps using INeedToInstallSomething<T>?
You can pass a dependencies list eg:
NServiceBus.Host.exe /install /dependsOn:"MSMQ,RavenDB"
The list needs to be comma delimmited.
In v4 the command line args are a bit different:
NServiceBus.Host.exe -install -dependsOn=MSMQ -dependsOn=RavenDB

WCF MSMQ DllNotFoundException

I am trying to access a remote WCF service (using netMsmqBinding) hosted in a windows service and am getting the error:
Message: System.TypeInitializationException: The type initializer for 'System.ServiceModel.Channels.Msmq' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'mqrt.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)
at System.ServiceModel.Channels.UnsafeNativeMethods.MQGetPrivateComputerInformation(String computerName, IntPtr properties)
I have read that this error may come up if msmq is not installed, but msmq is not supposed to be installed on the local machine... it is installed on the remote machine it is trying to talk to.
What else can cause this?
Any machine wishing to participate in the transmission of messages requires MSMQ to be installed.
This is because MSMQ uses a messaging pattern called Store and forward, which is what makes MSMQ robust to transmission failures.
Go to Programs and Features and then Turn Windows Feature on or off. Find Microsoft Message Queue (MSMQ) Server and enable it.
credit to: https://stackoverflow.com/a/26705197/782856

WCF Service Unexpected Exception on Endpoint

We created WCF services hosted on various windows services. During consumption of the WCF service by another windows service, initially everything goes successfully. But after continuous execution for a day, it suddenly throws the following exception:
The error message is Could not find endpoint element with name 'MemoryClient' and contract 'IQueue.IRepository' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.
The source of the exception System.ServiceModel
The stack trace of the exception:
at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
at System.ServiceModel.ChannelFactory1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
at System.ServiceModel.ChannelFactory1..ctor(String endpointConfigurationName)
at EngineChannelFactory.RepositoryClient.CreateNewChannel(String strEndPoint)
THE TARGET SITE OF EXCEPTION -> Void LoadChannelBehaviors(System.ServiceModel.Description.ServiceEndpoint, System.String)
The problem is, it is consuming this same end point for some time. Once the service restarts, the issue is resolved. Has anyone faced this issue? This is happening on Windows 2003 64 Bit Server, where .NET 3.5 is installed. Has someone said if this is an environment issue, and was fixed in .NET 3.5 SP1?
Thank you in advance.

Windows service connecting to other service over wcf crashes

I have two windows services. One ('server') acts as a WCF host to which the other ('client') connects. So I have configured a dependency from client to server. Both are also set up to start automatically.
When I start these services by hand, everything works fine. When I stop both services and tell client to start, then server will be started before client and all is fine.
However, when I reboot the machine only server is started.
When I add a diagnostic listener I see it got a TimeoutException with the helpful message:
The HTTP request to 'http://[server address]' has exceeded the allotted timeout of 00:00:00. The time allotted to this operation may have been a portion of a longer timeout.
At some other SO question there was an answer that claims WCF is probably confused about what went wrong and therefore starts lying about the timeout.
Did I perhaps miss a dependency for either service? Does WCF require something that hasn't or is being started when client is trying to contact server?
I think you should check your client service. On startup windows services are starting while network devices are still being initialized. Services should be ready to start without network and without any network device. Usual approach is to keep periodic retries to establish connection. You can do little experiment on your machine by uninstalling all network adapters and trying to start up your services.
Additional quick workaround you can do is to setup recovery options on your service -- for example you can configure it to restart service on crash after some timeout -- you can do this through UI in services.msc or in command line using 'sc config' command.
Configuring the dependency between the two Windows Services is not necessarily sufficient to avoid there being a race condition: i.e. to avoid the client service calling the WCF service before the server's WCF channel stack is fully initialised.
The service dependency just ensures that the Windows Service Control Manager won't start the client service process before the server Windows Service has notified the SCM that it has started. Whether this is sufficient depends on how you write the server.
If the server service starts a new thread on which to initialize the WCF stack, your OnStart method is probably returning before the WCF stack is ready for clients. There is then a race condition as to whether the client's first call will succeed.
On the other hand, if the server service does not return from OnStart (and thus doesn't notify the SCM that it has started) until the channel stack is fully open, the dependency removes the race condition, but there is a different pitfall: you need to beware that the SCM's own timeout for starting the Windows service is not triggered while waiting for the WCF stack to initialise, as might well happen on a reboot if the WCF service depends on the network stack, for example. If the server's OnStart does not return within the SCM's timeout, the SCM will not try to start the dependent client service at all, because it does not receive the server's start notification. (There will be a message in the Windows event log from the SCM saying that the server service didn't start within the expected time.) You can extend the SCM timeout by calling ServiceBase.RequestAdditionalTime while the WCF service is being initialised.
Either way, the client service really ought to be written so that it doesn't fail completely if the first WCF call doesn't succeed.
You don't actually say what binding you are using. If client and server services are always running on the same machine, as you seem to indicate, then consider using the NetNamedPipeBinding: then your service won't be dependent on initialization of networking resources and startup should be quicker.

Axapta 2009 WCF service

I am trying to add a service reference to axapta 2009. All is working well, its a simple web method(external webservice) that gets executed on the server tier(necessary, otherwise clr interop error)
But I've ran into the following problems :
is it possible to close the proxy one way or another? Because this option is not available in the generated service object in AX (only the webmethods and a tostring).
at a certain moment, i ran into a service with faulted state. Normally, you create the service object again, but this didnt solve anything, until i restarted the AOS. Is this normal behaviour? Is the service object cached or something like that on server side?
Thx in advance.
This is due to the fact that the WCF service is throwing Faults, probably unhandled faults.
Do you have access to the WCF Service? If so then have a look at this link: How do I prevent a WCF service from enter a faulted state?
Try to catch any exceptions within the WCF Service and log them.
Unfortunately Ax cannot catch FaultExceptions thrown by WCF so you will be limited to modify the WCF Service with an object encapsulating the return message, along with a flag if the method processed successfully or if an exception was thrown.
Yes it is normal behavior for a faulted WCF service to stay in the Faulted state. You may have to restart the IIS service or just recycle the AppPooll the WCF Service is running under.