Serilog not logging in .Net WebService ASMX WebMethod - asp.net

I have a .Net Web Application which I am successfully logging to file using Serilog and the File Sink. The main application is setting up the logger in the global.asax application start.
The web application assesses web services (also written by me) and I want to log from within the web-service (asmx) but I can't get it to write anything. I have tried multiple ways including setting up the LoggerConfiguration directly before calling the Log functions but with no luck. I have also enabled SelfLog and nothing is output.
Any assistance would be greatly appreciated - I've been googling this all day.

In order to get Serilog working in an ASMX web-service I had to declare a private static logger at the top of the service:
private static Serilog.Core.Logger myLog = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.File(HttpContext.Current.Server.MapPath("~/logs/QuoteEngine.txt"), rollingInterval: RollingInterval.Day).CreateLogger();
I then call:
myLog.Error("Your string") or myLog.Debug("Your string") etc
I hope this helps someone having same issues finding the answer.

Related

ASMX Web Service "Server did not recognize the value of HTTP Header SOAPAction"

I have a strange problem happening only when deploying an ASMX web service on the test server. I have a web service with one simple method:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class HarmonyCentralWebClassicService : System.Web.Services.WebService
{
[WebMethod]
public List<DeviceSync> GetDeviceSyncByCategoryAndSyncStatus(DeviceSyncCategoryId categoryId, DeviceSyncStatusId syncStatusId)
{
// Do something and return a list
return new List<DeviceSync>();
}
}
After deployment to a test server, when I call this method from a .NET client on another machine, I get the following error:
Caught SoapException thrown by
System.Web.Services.Protocols::SoapHttpClientProtocol.ReadResponse
Server did not recognize the value of HTTP Header SOAPAction:
http://tempri.org/GetDeviceSyncByCategoryAndSyncStatus
I've seen many SO articles and tried deleting Temporarily ASP Net folders and updating the web reference - these did not work (such as these here).
Does anyone have any idea?
Managed to figure out the problem. Very simple solution. Just goes to show if you come back after a few hours, you think more sensibly!
My client app.config was pointing to the wrong ASMX file and so obviously the run-time correctly observed that this action was not recognized by that particular web service.
If anyone faces a similar problem, a very simple check is to ensure you are actually pointing to the desired ASMX web service and that your web references in your client are up to date.
Of course, agree with John's comment - if you have the choice, stick with WCF or ASP.NET Web API.

Include WCF Service as part of an ASP.NET Application

I'm a little bit confused, I want a WCF service that takes values and returns them as XML, to be bound to an ASP.NET project. So I guess I should create WCF Service Library instead of Application, then bind it to ASP.NET via "Add Service Reference"? And, if I got it correctly, that allows me to use service without any proxy classes?
P.S. Service method code that returns XML is something like
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(
UriTemplate = "men",
ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Xml)]
Human[] getAll();
getAll() just generates an array of objects of Human class.
Actually, you've got it partially correct, and partially sideways.
Yes, use a WCF Service Library to create the service.
No, don't use "Add Service Reference".
Instead, add the WCF Service Library as a normal project reference to the ASP.NET web application. Then create a "YourService.svc" file as the endpoint for the service.
<% #ServiceHost Service="MyNamespace.MyServiceImplementationTypeName" %>
See "Deploying an Internet Information Services-Hosted WCF Service".
You will also need some configuration in web.config. I don't know the best way to do this.

HttpContext.Current.Server null

I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line:
reader = XmlReader.Create(HttpContext.Current.Server
.MapPath("~/TestDevice/Data.xml"), settings);
When control comes to this line. I get exception. I tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server is null. What is alternative syntax.
I tried to access this class library method from web application and it works fine.
Please suggest solution.
HttpContext.Current is returning null because your Windows Service is not running under the umbrella of IIS or some other web server provder.
However, you can find the executing path of your service using reflection:
System.Reflection.Assembly.GetExecutingAssembly().Location
^ should return the path of the executing service..
This method works much better:
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
It could be that when you are using windows service, you are no longer running a web app, therefore HttpContext and web server is not available. Try using System.IO.File for mapping, see whether that takes you to the correct directory.
Edit
private String yourFullPath = System.IO.Path.GetFullPath("/YourDirectory") + #"\";

Webservices help

We have a web service running on the server. We want to use the service in local machine. Could some one kindly give all the steps to get the methods availble in the client.
We have created web methods in the server. And trying to access the same thing on the client. I can literally access those methods using the refernce variable of the server. but when I try to run it , it comes up with run time exception unable to connect to remote server.
I have added the web reference to my client class. What else I am missing. Do I need to do any kind of registration of service with client from command prompt.
I am assuming the client is unable to connect to server because the server is not running when I try to access the methods.
Any one with guidance will be helpful.
Thank you
Hari Gillala
I have added web refernce to this below client class using http://ipaddressofwerver/decisionclass/decisionclass.svc
The code:
try
{
DecisionClass ds = new DecisionClass();
string s = ds.Url;
Label1.Text = s;
string [] a = ds.GetList();
foreach (string i in a)
{
Response.Write(i);
}
}
catch (Exception Ex)
{
Response.Write(Ex.Message);
}
I am assuming the client is unable to connect to server because the server is not running when I try to access the methods.
If it's not running, it won't generate a WSDL either. However, it may have been running while you created the web reference, and then stopped.
Here are some things you can try to track down the problem:
Open the web service's URL, as specified in the web reference, in a regular web browser. This should bring up the web service's documentation page, and if you're running locally and haven't changed the web service's web.config, you can even call some simple methods using the provided test forms
See if you can access the web service with SoapUI or a similar tool.
Also, make sure you're running the web service in IIS, not in the Visual Studio development server - IIS will keep running when you close the project or even Visual Studio, but the development server might not.

ASP.net web services

I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/resets
Can someone tell me if this is expected or can different webmethod calls from the same client can access the same Thread.CurrentPrincipal object
Thanks
As soon as you stop using a thread it goes back into the thread pool.
The next call will take a thread from the thread pool, but you have no control which one you get.
You need to send information about which user is making the call, with each request.
This is expected, every new web request is actually new thread. And every web request reset stuff like CurrentThread, CurrentCulture and so on.
What are you trying to do is authentication session. There are many possible solutions. But to suggest something I have to specify technology you use.
For example, ASP.NET ASMX Services can use Forms Authentication. Also they are aware about ASP.NET Session.
With WCF, you can enable ASP.NET support, so you will have same stuff like for ASP.NET ASMX Services. But you also can leverage on Windows Communication Foundation Authentication Service.
Anyways need more info from you.
If you are using the built-in ASP .NET authentication for your website and then just calling the web service from a web page, you may be able to enable session variables and user context information in the methods of the web service with a decoration. Like this:
[WebMethod(EnableSession=true)]
public void MyWebMethod()
{
string mySessionVar = HttpContext.Current.Session["sessionVar"].ToString();
IPrincipal currentUser = HttpContext.Current.User;
...
}
If that doesn't solve your problem, tell us what are you using the Thread.CurrentPrincipal object for (what you are pulling out of the Thread.CurrentPrincipal object). Perhaps there is another solution.

Resources