How to get WCF request in XML format? - wcf

I have created a WCF service and I am getting an appropriate output,
but I want to know what request I am sending?
service1 oc = new service1();
oc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
oc.ClientCredentials.UserName.UserName = UserName;
oc.ClientCredentials.UserName.Password = Password;
RM[] ass = oc.GetReasonMasterlist();

You can write a custom interceptor if you want to do your own thing, or enable trace logging as per https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/configuring-message-logging
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\logs\messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
</system.serviceModel>

Related

WCF Tracing log message is set to false, but there is still a trace output being generated

I have the following configuration
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="false" logKnownPii="false" logMalformedMessages="false" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="false" maxMessagesToLog="250000000" maxSizeOfMessageToLog="262144000">
<filters>
<clear />
</filters>
</messageLogging>
<endToEndTracing propagateActivity="true" activityTracing="true" messageFlowTracing="true" />
</diagnostics>
<system.diagnostics>
<sources>
<source propagateActivity="true" name="System.ServiceModel" switchValue="Off, ActivityTracing">
<listeners>
<add name="RollingDateXmlWriterTraceListener" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging" switchValue="Off">
<listeners>
<add name="RollingDateXmlWriterTraceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<!--<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\Logs\MainService.svclog" />-->
<add name="RollingDateXmlWriterTraceListener" type="XXX.Common.Listeners.RollingDateXmlWriterTraceListener, XXX.Common" traceOutputOptions="None" initializeData="C:\Temp\Logs\MainService.{yyyyMMdd}.svclog" />
</sharedListeners>
<trace autoflush="true" />
I have set false to all of the logMessage attributes, but i still get the svclog file. Do I miss something?

System.Diagnostics ServiceModel message logging not working for integration test project

I have an integration test project, that I run tests on my classes that consume an external WCF service
In MyApp.IntegrationTests I have an app.config file that looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
</connectionStrings>
<appSettings>
</appSettings>
<system.serviceModel>
<bindings>
-- ommitted for brevity
</bindings>
<client>
<endpoint name="IWhatever"
address="https://url.com/Whatever.svc"
binding="basicHttpBinding"
bindingConfiguration="basicHttpsBindingConfiguration"
contract="IWhatever" />
</client>
</system.serviceModel>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add initializeData="soap-log.xml" type="System.Diagnostics.XmlWriterTraceListener"
name="messages" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
As you can see, I'm trying to log my calls to soap-log.xml
If I run my tests, no log file is created.
What is confusing me is, if I have the system.diagnostics block in my actual web app project, the xml log file is created.
It's seems you missed messageLogging element. MSDN link.
<system.serviceModel>
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="20000"/>
</diagnostics>
</system.serviceModel>

Implementing Queueing in WCF

I have a simple WCF service which creates a directory at a location specified as a param.
The service is hosted as a Windows service and runs under the administrator account.
The InstanceContextMode is Single and so is the Concurrency.
The method returns a string of the directory location after creating it.
A client is accessing this service from another computer using ASP.NET.
He has a use case, he clicks the button on the page multiple times in a matter of few seconds, like clicking it 5 times in 2 seconds. What's happening is that the service creates only 2 or 3 folders not all 5.
I know that there is a default queueing mechanism in WCF and for my service it's either not working or I haven't written it to use that mechanism.
How do I go about solving this problem.
Here's the ASP.NET code for button click
protected void Button1_Click(object sender, EventArgs e)
{
ClientClass objClientClass = new ClientClass();
string returnValue = string.Empty;
Random random = new Random();
string uid = random.Next().ToString();
returnValue = objClientClass.StartWorkflow(uid);
Label1.Text = returnValue;
}
Here's the app.config of the host:
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="All"
propagateActivity="true">
<listeners>
<add name="traceListener" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging"
switchValue="All"
propagateActivity="true">
<listeners>
<add name="traceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="Traces.svclog" />
</sharedListeners>
</system.diagnostics>
<system.web>
<compilation debug="true"></compilation>
</system.web>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true"
logMessagesAtServiceLevel="false"
logMessagesAtTransportLevel="false"
logMalformedMessages="true"
maxMessagesToLog="5000"
maxSizeOfMessageToLog="2000">
</messageLogging>
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceThrottling maxConcurrentCalls="1" maxConcurrentSessions="5" maxConcurrentInstances="5" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="Utilities.WS.SampleWebService.Jobs">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
contract="Utilities.WS.SampleWebService.IJobs" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://00.00.000.00:8732/Utilities.WS.SampleWebService.Jobs" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Regards.
STEP 1. ping from client (Which is server side ASP.NET code, not the browser), can it reach the machine hosting the WCF service? Firewall sitting between your webserver and where WCF is hosted?
STEP 2. SVC log
Setup svclog from your config file,
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Then use SvcTraceViewer from command prompt - look for red.
STEP 3. Add some more logging to your operation and see if anything actually executed
http://msdn.microsoft.com/en-us/library/ms732023(v=vs.110).aspx

Logging Request and Response values for a WCF request in client

I am trying to log the request ad response messages in my client while the client is consuming a thirdparty WebService.
Despite of many attempts the log file - MessageLog.svclog never gets created using the following WCF configuration in the app.config file. Please help.
<system.diagnostics>
<sources>
<source
name="System.ServiceModel.MessageLogging"
switchValue="Information, ActivityTracing" >
<listeners>
<add name="yourTrace"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\Log\MessageLog.svclog">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<trace autoflush="true" />
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging
logMessagesAtTransportLevel="true"
logMessagesAtServiceLevel="false"
logMalformedMessages="true"
logEntireMessage="true"
maxSizeOfMessageToLog="65535000" maxMessagesToLog="500" />
</diagnostics>
</system.serviceModel>
Thank you !
Try this. Notice the SharedListeners node
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging"
switchValue="Warning, ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\Log\MessageLog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
...
</system.serviceModel>
I had problem with WS Client tracing where App.config and folder permissions were totally ok. The solution in my case was that I renamed it to .exe.config (was built to .dll.config of the WS client dll). So make sure:
That the name in the folder of the config file matches the assembly name
That when you run/debug it, the assembly that config file is for, is really used and not assembly for other location.
If it is built into .dll.config it is maybe ignored cause it should be .exe.config.
-m

WCF: How to do "Add Service Reference" to an SSL Service (I'm getting errors)

FooService.svc.cs:
[ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IFooBar
{
[OperationContract()]
int Add(int num1, int num2);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class FooService : IFooBar
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}
Web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
<globalization culture="en-US" uiCulture="en-US" />
<compilation defaultLanguage="c#" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<defaultDocument>
<files>
<add value="index.aspx" />
</files>
</defaultDocument>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
<source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp" />
</sharedListeners>
</system.diagnostics>
</configuration>
I published the Files to my server (which has a ssl certificate on it), but when I go to the webpage: https://localhost:443/FooService.svc I get the following error...
The request message must be protected.
This is required by an operation of
the contract
('IFooBar','http://tempuri.org/'). The
protection must be provided by the
binding
('BasicHttpBinding','http://tempuri.org/').
This happens whether I go to the url directly or if I go to Add Service Reference... from within Visual Studio.
Also, if I change the [ServiceContract(ProtectionLevel = ProtectionLevel.EncryptAndSign)] to [ServiceContract] it works fine.
The default binding for the HTTP transport in WCF 4 is basicHttpBinding. When you set the service contract protection level to encrypt and sign then the binding must also support message level security. Since basicHttpBinding does not support message level security, you need to configure the service manually to wsHttpBinding or change the protocol map as follows:
<system.serviceModel>
<protocolMapping>
<remove scheme="http" />
<add scheme="http" binding="wsHttpBinding" bindingConfiguration="" />
</protocolMapping>
... snip ...
</system.serviceModel>