Error : HttpContext.Current is null. |PerWebRequestLifestyle | Castle Windsor | Threading - c#

I have WCF service and inside this service using MEF i am calling runtime class library.
I have implemented DI(castle Windsor) in this class library and it work ok in all request but instantly some time it return error like below
"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net"
We have apply all internet solution like install Asp.net feature, change DI Lifecycle from LifestylePerWebRequest to scope , hibridgeLifeStyle etc
But not found proper result below is my code
Can any one plese help me solve this issue. As i am new in DI and don't this is Life cycle issue or MEF issue or threading issue because i am calling this class libreary in to thread
Thanks in advance

You need to run WCF in ASP.Net compat mode to get access to HttpContext. Not sure why you need it though because then you can only run the WCF services on IIS in asp.net compat mode and not within a different hosting environment like a windows service.
more information here: https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet which also tells you about contexts you should be using instead of HttpContext

Related

Refreshing loaded Assemblies during WebApi runtime

I have a C# Owin WebApi Selfhost Project and the controllers I use are implemented in separate assemblies.
I´m finding these assemblies by naming conventions before I start up the WebApp and load them into the AppDomain. Then when the Api starts, the controllers are accessible. There is another approach which involves a custom IAssemblyResolver-class which replaces the default one in the Config.Services
config.Services.Replace(typeof(IAssembliesResolver), new MyAssembliesResolver());
This also works, all my seperate controller-assemblies are found, loaded and accessible.
Now to the problem: It may occur, that a new controller-assembly appears in my executing directory. My Api has an "AssembliesController" which can be told to look for new assemblies in the executing directory and load them
during runtime. This also works, but the problem is: the controllers in the newly loaded assembly aren´t accessible until I restart my Api.
It appears that the Api just asks once for assemblies (IAssembliesResolver) and available controller types (IHttpControllerTypeResolver) on startup and works with the results until the end. But in my case I want to add assemblies/controllers during runtime without restarting the Api. Can somebody help me please? How do I get the Api to refresh the assemblies/controllers?
You need to overwrite the DefaultHttpControllerSelector.
Here is a very nice article on the subject: link
Enjoy!

ASP.NET Web API with Linq2Couchbase dependancy injection

I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?
Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.

Error when trying to add a Service Reference

I´m trying to create a client in C# to a web service which (I suppose) is written in Java. It´s my first time trying to write a client, so I´m following the instructions on MSDN, but I´m stuck on a problem with Add Reference. When I open the Add Service Reference dialog and add the URL, an error occurs:
There was an error downloading 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
There was no endpoint listening at http://geoportal.cuzk.cz/WCTService/WCTService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
What should my next step be? I don´t know what I should do with this!
(It is a coordinates-transformation service from the Czech Republic.)
For more information:
Property services (GetCapabilities)
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?
Localization services:
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?request=GetCapabilities&service=WCTS
I was facing a similar situation in which I had created a WCF Service (Employee.svc) and later changed the named to EmployeeService.svc. WCF project compiled just fine but when I was trying to add service reference from by UI Client, I was getting following error:
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc?wsdl'.
The document format is not recognized (the content type is 'text/html; charset=UTF-8').
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc'.
There was no endpoint listening at 'http://localhost:2278/EmployeeService.svc' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
I resolved it by replacing the correct service class name everywhere. In my case, it should have been EmployeeService and NOT employee. The left out place was in the markup code of svc file:
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**Employee**" CodeBehind="EmployeeService.svc.cs" %>
Changed it to
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**EmployeeService**" CodeBehind="EmployeeService.svc.cs" %>
And it started working again!!! Dont forget to build your WCF project after changing the service name.
I tried browsing to http://geoportal.cuzk.cz/WCTService/WCTService.svc?wsdl. It looks like this service is not exposing metadata.
I did a bit of googling on OpenGIS, and I think you need to have a look at this article:
OpenGIS with .NET
You won't be able to just add a service reference and go. It looks like you need to craft a concrete WSDL.
There may be a client-side library you can use / customize to assist with integration. Have a look at Stack Overflow question Using MySQL GeoSpatial data types in .NET.
I know this is an old thread and has already been resolved, but I just finished troubleshooting this exact issue, and none of the resolutions presented here worked for me. Wanted to share my resolution in case anyone else runs into this thread with a similar issue.
My ENTIRE issue stemmed from a bad Refactor->Rename operation. I recently purchased Resharper for my dev team and Resharper did not like the name of our service implementation name. We had named it "WCFAccess" and Resharper wanted the name "WcfAccess". I had just published an update, had the release safely isolated in its own release branch in git, and figured this was a good time to perform a rename on the develop branch and shut Resharper up about the naming. I used Refactor->Rename to change the name of the file to match the naming convention we had defined in the Resharper configuration. The rename operation completed, the solution compiled and ran, time goes on and the WCF rename was forgotten.
Fast forward a couple weeks, and its time to deploy out web services to the test environment for regression testing. The solution compiled successfully, published successfully, then gave me the EXACT error that the OP posted. What I ended up finding out is that the Rename operation from weeks ago ONLY UPDATED SOURCE CODE REFERENCES to the old name and did not rename MARKUP. When I navigated out to our web server where the service was published to and double clicked on the .svc file, it opened the markup in Visual Studio and I noticed that the character casing of the CodeBehind="ServiceNameHere.svc.vb" was inconstant with the new naming convention. Updating the markup and web.config files to reference the correct character casing resolved my issue.
I hope this helps someone. It was incredibly frustrating to troubleshoot
(Please don't hate me for using VB.Net, I inherited this application) :-)
Is the service definitely up and running before you try to add a service reference? If it exposes meta data, does it have a service behaviour or equivalent configured? Have you configured your firewall correctly?
While adding service reference to the client application, metadata is not accessible from service to client application. hence The remote server returned an error: (404) Not Found. Actually we can host the WCF service as follows:
Self hosting(console application)
IIS Hosting
WAS Hosting
window service hosting.
if you are using self hosting then you need to host the service in console application and run the service(run the console application) and then add the service reference to the client application, then metadata would be exchange. If service is not running then while adding service reference to the client application then 404 not found error would be getting. Same process would follow for all the hosting type. first run the service then add service reference.
I had the same problem happen to me earlier today. The webservice was running fine on local host but for some reason, I was having a 400 when trying to add the service reference in another project.
My error was caused by setting the [DataMember] annotation instead of the [EnumMember] annotation on an enum of the service. Changing it solved my issue.
The webservices doesn't run.
If you don't have access to the server where this service run, you're blocked.
Otherwise, you need to check if the server run, etc. As I don't know how the Java webservice is run, I can't help you further.

Silverlight -> WCF -> Database -> problem

I have some silverlight code that calls a WCF service which then uses the Entity Framework to access the database and return records.
Everything runs fine but ... when I replace the Entity Framework code with classic ADO.NET code I get an error:
The remote server returned an error: NotFound
When I call the ADO.NET code directly with a unit test it returns records fine so it's not a problem with the ADO.NEt code
I used fiddler and it seems to say that the service cannot be found with a "500" error.
i don't think it's anything to do with the service as the only thing I change is the technology to access the database.
Anyone know what i'm missing here?
'NotFound' is a generic error message that could mean just about anything. If you are absolutely positive that you haven't changed the service interface, then the likely candidate is an exception is being thrown from within your service. Are you sure that the collection type that contains the data you are trying to return hasn't changed, i.e. from List< OfSomthing> to List< OfSomethingElse>?
In any case, something i have found invaluable for tracking this sort of issue is the Service Trace Viewer tool from Microsoft. Read all about it right here, all it takes is some simple changes to your web.config to enable the logging.
John Papa has a great article from MSDN Magazine Data Performance and Fault Strategies in Silverlight 3 that explains this issue and offers a solution. Due to browser limitations, error code 500s aren't routed properly. His solution modifies the outgoing HttpResponse Message back to a 200 for all Silverlight 500 responses.
If you are still stuck, you may want to try making the call to the service from the machine hosting the service. IIS by default returns much more information about what might have gone wrong when the call is from the local machine. (I believe this can be changed, but don't know for sure.)
try to add
HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
to the Application startup event of the Silverlight app.
It should give you detail of what the real error is rather than NotFound. In my case, i was missing the clientaccesspolicy.xml resuired for cross domain requests.
How to: Specify Browser or Client HTTP Handling

Using Webservice classes in Silverlight when adding service reference instead of web reference

Scenario:
I am using Silverlight 3.0 as client for a web service.
Design:
The server has a class named DeviceInfoService which has the basic functionality of getting the list of devices, getting the properties of devices etc.
When I open an ASP.NET project and try to add a web reference, I can find an option to add a "Web Reference". After I add the web reference this way, I am able to access the DeviceInfoService class by creating it's object and then accessing it's methods.
Web Reference v/s Service Reference:
Coming to Silverlight: when I try to add a service reference, there is no option to add a web reference. Going with Service Reference, everything works fine till WSDL file is downloaded. People say that I can get this option by going back to .NET 2.0, but probably Silverlight won't work in .NET 2.0
The Problem
Now when I try to access the class DeviceInfoService , I am not able to find it. All I get is Interfaces -- DeviceInfoServiceSoap and DeviceInfoServiceSoapChannel. Classes named DeviceInfoServiceSoapClient.
The methods GetHostedDevices and GetDeviceInfo are no longer available. All I get is GetDeviceInfoRequest, GetDeviceInfoRequestBody, GetDeviceInfoResponse and GetDeviceInfoResponseBody.
I googled a lot how to use these four classes, only to find nothing. I want to get those 2 classes directly like in ASP.NET and not using those Request Response type.
You sound awfully confuse about some concepts.
How about you watch the following Silverlight.Net video and see if that helps?
How to Consume WCF and ASP.NET Web Services in Silverlight
What is web reference in ASP.NET is equivalent to service reference in Silverlight.
Here's an example of how to use a web service in Silverlight, e.g. the CDYNE Profanity Filter.
Add a new Service Reference to your project, URL is: http://ws.cdyne.com/ProfanityWS/Profanity.asmx?wsdl, leave the name as ServiceReference1.
Use this code behind to call the service (which was implemented to be asynchronous):
public MainPage()
{
InitializeComponent();
string badText = "I wonder if the filter will filter this out: shit bad luck";
ServiceReference1.ProfanitySoapClient client = new ServiceReference1.ProfanitySoapClient();
client.ProfanityFilterCompleted += new EventHandler<ServiceReference1.ProfanityFilterCompletedEventArgs>(client_ProfanityFilterCompleted);
client.ProfanityFilterAsync(badText, 0, false);
}
void client_ProfanityFilterCompleted(object sender, ServiceReference1.ProfanityFilterCompletedEventArgs e)
{
string cleanText = e.Result.CleanText; // Web service callback is here
}
And you've got a web service up and running in Silverlight!

Categories

Resources