Log WPF Data Binding Errors With Log4Net - wpf

It appears I the data binding errors can be logged via Bea Stollnitz blog entry.
How can I send Data Binding errors through log4net instead of TraceSources or at the very least route the TraceSource to log4net?

Implement a trace listener that writes log messages with log4net. An example can be found here:
How to log Trace messages with log4net?
EDIT (Configuration Example):
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="Log4netTraceListener" type="NameSpace.YourClass, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...." />
</listeners>
</trace>
</system.diagnostics>

Related

Custom HttpHandler is never loaded in IIS 10

We're moving a site to a new server, and running into an issue with a custom HTTP Handler. The old server was IIS 6.1, the new server is IIS 10 (Server 2019).
The handler is meant to handle all requests under a certain directory path (which does not physically exist on disc), and retrieve the requested files from Sql Server, where they are stored as bytes. The handler itself is super simple; while I don't think it is relevant, here is the code for the handler, which is located in the App_Code folder.
Public Class GetCaseUpload
Implements IHttpHandler
Public ReadOnly Property IsReusable As Boolean Implements IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim url = context.Request.Url
Dim guid = url.Segments()(url.Segments.Length - 2).Trim("/".ToCharArray())
Dim dbo = GetDbObj()
Dim data = dbo.ExecuteDR(<s>SELECT * FROM dbo.UPLOADS WHERE GUID = <%= dbo.FixString(guid) %></s>)
If data Is Nothing Then
context.Response.ContentType = "text/plain"
context.Response.Write("The requested upload could not be found." & vbCrLf & "GUID: " & guid)
Else
context.Response.ContentType = data("FILETYPE")
context.Response.BinaryWrite(data("FILEBYTES"))
End If
End Sub
End Class
The pipeline is running in integrated mode. Here is the current Handler mapping in system.webServer:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="GetCaseUploadFile" path="*/cases/uploads/*" verb="GET" type="GetCaseUpload" resourceType="Unspecified" preCondition="integratedMode" />
... others removed for brevity ...
</handlers>
</system.webServer>
I just flat-out deleted the handler entries from the old server, and readded it through the Add Managed Handler dialog in the Handler Mappings section of the site in IIS management console, and that is what it created.
Also, since these files are not ones normally handled by IIS (PDF, JPGs, DOCs, etc.), I've tried adding
<modules runAllManagedModulesForAllRequests="true" />
Although it was working on the old server without needing that.
Any time I try to pull up a file from that directory though, it always returns a 404.0 error, and shows that the request was mapped to the Static File handler. I've turned on Failed Request Tracing, and see the logs for the requests, and nowhere in the details does it ever even mention that it loaded my handler. If I search through the raw XML file, the name of my class is not found at all.
Here is the detailed error screen being shown:
You need to put the handler code in web.config file as suggested in the below section:
<handlers>
<add name="MyHandler" verb="*" path="myhandler.api" type="MyAssembly.MyHandlerClass, MyAssembly"/>
</handlers>
If your application pool running in Classic mode, then the handler reference needs to go into the following section:
<system.web>
<httpHandlers>
<add verb="*" path="myhandler.api" type="MyAssembly.MyHandlerClass, MyAssembly"/>
</httpHandlers>
</system.web>
If your application pool running in Integrated pipelined mode, then the handler reference needs to go into the following section:
<system.webServer>
<handlers>
<add name="MyHandler" verb="*" path="myhandler.api" type="MyAssembly.MyHandlerClass, MyAssembly"/>
</handlers>
<system.webServer>
so first check your application pool mode and set the handler code.
I had the same issue. Service that implements IHttpHandler running fine on IIS 6 migrated to IIS 10 and needed to work out the required config tweak in order to overcome migrated service getting 404.
Answer provided by Jalpa Panchal did the trick.

System.Threading.ThreadAbortException in classlibrary

I have a wpf application which will call a class library as a plugin to connect with wcf service every time. When call a service method in class library, it will give the above exception. And the exception message is
er = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}Even the stack trace also has the above message. What is the solution?
I'm just guessing here, as you don't provide much code. But in your main application make sure to add the following eventhandlers;
DispatcherUnhandledException
AppDomain.CurrentDomain.UnhandledException
TaskScheduler.UnobservedTaskException
As also documented in this thread:
http://stackoverflow.com/questions/1472498/wpf-global-exception-handler
Log your exceptions with something like Log4net. Regarding WCF, all calls should be wrapped in a try catch block.
If your error is WCF related you can take your service's app.config file and add the following configuration:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource"
switchValue="Information, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\temp\wcf.svclog" />
</sharedListeners>
This will log all WCF activity to c:\temp\wcf.svclog which you may view using the tool SvcTraceViewer.exe located in your win sdk directory.
Hope it helps
Stian

Connecting to a deployed WCF on IIS with Silverlight

I've deployed a WCF service on IIS 7.
Browsing to the service from a browser shows that it's running fine.
Connecting to the service from a Silverlight application produces the following error:
The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://localhost:81/SdkService.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:81/SdkService.svc. The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
If the service is defined in the current solution, try building the solution and adding the service reference again.
The service project is complied with .Net 4.0 and the Application pool for that service is also .Net 4.0
The Silverlight client is also compiled with .Net 4.0
Here's the service web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="6553600"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
enter code here
Silverlight only supports basicHttpBinding. I believe the WCF default is wsHttpBinding so you may need to change this and then update your service reference.

Calling WCF service with forms authentication

I have wpf application that calls wcf service with forms authentication.
I added Service Reference to my solution, and in service configuration checked Generate asynchronous operations.
In code behind I created client to that reference,
than I attached event to fire after async method is completed,
then username and pass is setted:
client.ClientCredentials.UserName.UserName = txtUser.Text;
client.ClientCredentials.UserName.Password = passbox.Password;
client.IsAuthenticatedAsync(txtUser.Text, passbox.Password);
client.Close();
in IsAuthenticatedCompletedEventArgs I get error:
"The communication object, System.ServiceModel.Channels.HttpsChannelFactory+HttpsRequestChannel, cannot be used for communication because it has been Aborted."
Does anyone knows why this happens?
Thanks in advance!
To be honest, I haven't had a chance to test your scenario, but that error is a general error stating something went wrong while communicating with the service (this answer is more of a comment, but it's extensive and can't fit). You could get more info by putting the following lines at the end of the <configuration> section in your web.config and app.config:
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="sdt"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "ServiceTestWCFTrace.svclog" />
</listeners>
</source>
</sources>
After you get the error use svc log viewer to view the log: C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcTraceViewer.exe
There you'll probably find the exact error that caused the Abort on the channel. Hope it helps.

Error: Only LDAP Connection Strings are Supported against Active Directory

I have the following ASP.NET Membership section defined in the Web.config file:
<membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
<providers>
<clear/>
<add connectionStringName="ADService" connectionUsername="umanage"
connectionPassword="letmein" enablePasswordReset="true" enableSearchMethods="true"
applicationName="uManage" clientSearchTimeout="30" serverSearchTimeout="30"
name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</membership>
The Connection string looks like this:
<add name="ADService" connectionString="ldap://familynet.local" />
Whenever I call the following code:
Membership.GetAllUsers();
I get the following error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Only LDAP connection strings are supported against Active Directory and ADAM.
I don't understand why the system is claiming the LDAP connection string is bad because it is in fact a valid LDAP string as specified by the MSDN documentation.
http://msdn.microsoft.com/en-us/library/system.web.security.activedirectorymembershipprovider.aspx
Any ideas?
Your LDAP connection string actually is NOT valid - LDAP connection strings are case-sensitive; the LDAP prefix must be uppercase. Use:
connectionString="LDAP://familynet.local" />
and you should be fine.

Resources