Remove ActivityId from WCF Request - wcf

I'm having an interesting compatibility issue between a WCF client and a Java web service. In short I've found that the way the header is generated is causing the problem - the ActivityId and Action elements in the header as well as what WCF is doing with the namespace of the custom header is causing issues. I've successfully consumed the WSDL with wsdl.exe, but WCF seems to be manipulating the header in a way that the Java web service doesn't like. Is there any way I can set up the bindings for the WCF client to not send the ActivityId and Action elements?

Do you have tracing turned on in the client? I think that is what is adding the activity ID as its trying to flow the tracing activity to the service for end to end tracing. Turn off the activity tracing flag and it should go - see my comment for the action header

This issue commonly occurs when a WCF client attempts to connect to a non-WCF server, e.g. JAX-WS, Websphere etc.
Just to add to Richard's lifesaver answer and address #irperez's comment, the actual settings which need to be disabled to prevent WCF diagnostics from adding ActivityId during WCF Diagnostic Tracing are to remove:
Remove ActivityTracing from switchvalue
Set propagateActivity to false
i.e. Change
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml"/>
</listeners>
</source>
...
To:
<source name="System.ServiceModel" switchValue="Information"
propagateActivity="false">
<listeners>
<add name="xml"/>
</listeners>
If the ActivityId is enabled, it injects the below into the SOAP headers, which can break unsuspecting servers:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="5de75017-da08-4ac2-84f2-5374953cc2a1"
xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">
9f076849-e76e-4675-84c1-5026b1c2eb1a
</ActivityId>
</s:Header>

Related

WCF There was an error reading from the pipe: The pipe has been ended. (109, 0x6d)

I'm having continuous exception on live server and performance issues.
After enabling trace service log in wcf config file I found 100 of same exceptions.
Basic Information
Activity ID {00000000-0000-0000-0000-000000000000}
Time 2019-08-26 19:59:44.7454
Level Error
Source System.ServiceModel
Process w3wp
Thread 55
Computer PC3
Trace Identifier/Code https://learn.microsoft.com/dotnet/framework/wcf/diagnostics/tracing/System-ServiceModel-Diagnostics-ThrowingException
Exception
System.IO.PipeException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Exception Type
System.IO.PipeException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message
There was an error reading from the pipe: The pipe has been ended. (109, 0x6d).
Here is the code I'm using common code
public static WMC.Proxy.BLLService.BLLServiceClient GetBLLServiceClient()
{
var client = new WMC.Proxy.BLLService.BLLServiceClient(settings.GetBinding(settings.BLLServiceBinding), new EndpointAddress(settings.BLLServiceAddress));
SetMaxGraphInItems(client);
return client;
}
How can I fix this?
After a long search on the internet, I found the issue might be relates to the Enum type, which caused serialization failure.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/ee75d768-fd80-4c2b-831e-1d6dd6d4dd17/there-was-an-error-reading-from-the-pipe-the-pipe-has-been-ended-109-0x6d?forum=wcf
WCF NamedPipe CommunicationException - "The pipe has been ended. (109, 0x6d)."
There are many factors contributing to this problem, including data contract, service contract irregularities. It boils down to the fact the there is something wrong with the serialization. Please refer to the below discussion, wish it is useful to you.
http://gonetdotnet.blogspot.com/2014/07/solved-there-was-error-reading-from.html
https://blogs.infosupport.com/there-was-an-error-reading-from-the-pipe-unrecognized-error/
Feel free to let me know if the problem still exists.
Just to be sure, here's web.config section for the trace listener:
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing"><listeners><add name="xml" /></listeners></source>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"><listeners><add name="xml" /></listeners></source>
<source name="CardSpace"><listeners><add name="xml" /></listeners></source>
<source name="System.IO.Log"><listeners><add name="xml" /></listeners></source>
<source name="System.Runtime.Serialization"><listeners><add name="xml" /></listeners></source>
<source name="System.IdentityModel"><listeners><add name="xml" /></listeners></source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="D:\PutYourPathHere.svclog" />
</sharedListeners>
</system.diagnostics>
Produced file can be inspected using Microsoft Service Trace Viewer.
Try to inspect other events as well. There may be some Contract related issues causing serialization to fail.
If you are absolutely sure that your contract and serialization are fine, then try to reboot "Net.Pipe Listener Adapter" service.
If that does not help, try recycling app pool.
We can see theese issues time to time after new releases or config updates. They are related to wrong communication termination of previous instances.

Log SOAP message (response created by server) into database on server side

I want to log Outgoing (SOAP) messages generated by Web service in response of request sent by client (on server side).
Using Service model clients are able to log the response into the database by applying settings into the web.config of the web application e.g.
< system.servicemodel >
< extensions>
< behaviors>
< bindings> ...
Please guide me how to achieve logging and configuration on server side.
You can enable diagnostic tracing :
Just need to add a section in web.config -
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "F:\Sony\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
http://sonyarouje.com/2011/11/10/diagnostic-trace-svclog-configuration-for-wcf/
http://msdn.microsoft.com/en-us/library/ms732023.aspx
Also check this related question How to turn on WCF tracing?
To see the actual SOAP envelope that is passed around
http://litemedia.info/debug-soap-request-and-response-in-wcf
Also found one more similar question How can I enable WCF logging so that it writes to a Database?

WCF service slow response after less than 5 minutes idle (4.0 on Azure)

I have a WCF service with 2 endpoints using BasicHttpBinding running on an instance in Azure. When the service is called for the first time it takes 13-16 seconds for a response (which is to be expected) and subsequent requests are adequate at between 150ms and 1000ms depending on the request type (I am using soapUI for testing).
But if I do not send any requests for a few minutes (less than 4 or 5 minutes) the service instance does not appear to be getting reused as it is taking as long as the first request of 13+ seconds.
I have tried "InstanceContextMode = InstanceContextMode.Single" in my ServiceBehavior(s) but that did not fix the problem.
In the trace I can see that the thread id changes whenever the service gives a slow response.
I have also logged the time from when Application_BeginRequest is hit to the start of the method being called by the client which seems to be the largest delay during these 'slow' calls.
I have some fairly large classes representing business interfaces and am using Entity Framework for a MySQL database running on an Azure VM. (Although I don't think this is necessarily an Azure issue).
The slow responses are the same whether I am calling a complex message type linked to DB operations or just a simple "Hello World" response.
Even if a new instance of the service is required should it take as long as an initial load after an IIS restart or application pool recycle?
Would doing some magic using wrappers for Static classes help? (I'm thinking not).
Built with C# 4.0 / VS2010 Professional
There can be lots of reasons why this is happening. I suggest you start with some tracing in your WCF service.
Add some logging in your application using TraceSource:
private TraceSource ts = new TraceSource("MyApp");
public string GetData(int value)
{
ts.TraceInformation("GetData called with {0}", value);
return string.Format("You entered: {0}", value);
}
Configure the TraceSource in your web.config together with the System.ServiceModel source.
<system.diagnostics>
<sources>
<source name="MyApp" switchValue="All">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel" switchValue="All" propagateActivity="true">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="Logs.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
Open the svclog file and you'll be able to see a detailed overview of everything that's happing in your service and how long each little step takes. This should help you to poinpoint the issue.

Ignore certain WCF services from WCF trace logs

The application I am building exposes several WCF services (A, B). Internally, it consumes several other WCF services running on our internal network (X, Y).
Using WCF message logging, I wish to only log traffic between our services A, B and the external clients who call them.
No data between my services (A,B) and the backend services (X,Y) should be logged by WCF.
Filtering via system.serviceModel/diagnostics/messageLogging/filters was partially successful with:
<filters>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
/s:Envelope/s:Header/*[contains(text(),"MyServiceA")]
</add>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">
/s:Envelope/s:Header/a:Action[contains(text(),"MyServiceA")]
</add>
</filters>
This however fails to capture responses from our service, as SOAP responses do not contain text to filter on.
The WCF MessageLogTraceRecord does contain the SOAP Action, but I can not seem to construct a filter to access it:
<MessageLogTraceRecord>
<Addressing xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace>
<Action>http://opia.api.translink.com.au/ApiLocationService/2012/04/IApiLocationService/ResolveInputServiceFaultFault</Action>
</Addressing>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
...
WCF message logging and end to end tracing are enabled with all options set to true. ActivityTracing and Warning level logging are enabled.
Try this
<filters>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
/s:Envelope/s:Header/*[contains(text(),"MyServiceA")]
</add>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">
/s:Envelope/s:Header/a:Action[contains(text(),"http://opia.api.translink.com.au/ApiLocationService/2012/04/IApiLocationService/ResolveInputServiceFaultFault")]
</add>
</filters>
Replaced the text "MyServiceA" in action filter with the URL in MessageLogTraceRecord > Action

WCF logging messages

I have a WCF service (hosted by IIS webpage) that is working but I want to write more info out to a log file.
I inherited code and it had some simple logging to a file. That worked on the original developers machine but I don't get anything written out. Note it reads the log file path from the config which I assume was the web config but just in case I hard coded the location for now "c:\temp\logfile.log".
I gave full permissions to temp to ASPNET and then USERS thinking it was a permissions issues.
Also I noticed there was Tracing and Message logging built into WCF. Tried that but not sure if I'm doing it correctly.
Any ideas about the simple file writing?
Should I be doing the built in logging and if so any simple examples?
Thanks!
To configure Logging:
Configure Logging
1. In the Configuration Editor, select the Diagnostics node.
2. In the right pane, click Enable MessageLogging.
This will create ServiceModelMessageLoggingListener and
System.ServiceModel.MessageLogging nodes under the Listeners and Sources
folders, respectively.
3. In the left pane, select MessageLogging under the Diagnostics node.
4. Set the LogMessagesAtServiceLevel attribute to True by choosing this option
from the drop-down list.
5. In the left pane, select ServiceModelMessageLoggingListener under the
Listeners node.
Note the default value of the InitData attribute, which is set to
c:\inetpub\wwwroot\WCFService\web_messages.svclog, the location where the
message will be logged.
via WCF Security Guidance.
Use the following in <system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
and use the following in <configuration>
<system.diagnostics>
<sources>
<source name ="System.ServiceModel" switchValue="Information, ActivityTracing">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name ="System.ServiceModel.MessageLogging"
switchValue="Verbose, ActivityTracing">
<listeners>
<add name="xml" />
</listeners>
</source>
<source name ="System.Runtime.Serialization" switchValue="Verbose">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener"
traceOutputOptions="LogicalOperationStack"
initializeData="C:\logs\RestAPISvcLog\Traces.svclog" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
This will create log file and you can view directly that with svctracelog viewer (which comes with VS).
For more info refer this -- http://msdn.microsoft.com/en-us/library/ms730064(v=vs.110).aspx
Use the EventViewer logging instead, but you will have to make sure your installer can create the appropriate EventViewer source.
Maybe you should use that Configuration Editor-Tool "SvcConfigEditor.exe" (see description on MSDN). This allows to configure WCF-logging in a quiet easy and structured way.
The log files can then be analysed with the Service Trace Viewer-Tool "SvcTraceViewer.exe" (see on MSDN).
Both tools are bundled with Visual Studio an can be downloaded from Microsoft.
To write to the log file, make sure that the identity running your web application has write access to the log directory.
IIS7:
You can find the identity in the IIS management console. Select the application pool that your web application is using. Click on Advanced Settings. In the properties window, look for the identity field. It may say Network Service. This is the account that needs write permission to your log output folder.
IIS6:
Same as IIS7 except right click on the app pool and select properties. The properties window of IIS6 will have an Identity tab.
If you already have a log file in this directory, try deleting it and letting the framework create it.
Hope this helps.