Referencing Projects from Windows Service - c#

I am in the middle of trying to write my first windows service using C# and .NET framework and am having trouble referencing a project within the same solution within the service. My MVC solution structure within VS2012:
1 MVC project (web app project)
1 additional processing project (used for extended processing within controllers)
1 service project
1 install shield le project (successfully installs the service project)
Within my service (which I want to fire every x mins) I am referencing methods in the extended processing project that intellisense is able to detect when I am writing code within the service project (I have a using extendedProcessingProject; using webAppProject; and a project references to the extendedProcessingProject and web app project).
The service installs to my local pc without errors. When I start the service I have an error being logged that I cannot seem to figure out:
Exception body: ReminderEmailService Object reference not set to an instance of an object. System.Collections.ListDictionaryInternal
System.NullReferenceException: Object reference not set to an instance of an object.
at ReminderEmailService.ReminderEmailService.ReminderEmails(Object source, ElapsedEventArgs e)
at ReminderEmailService.ReminderEmailService.SendReminderEmails(Object source, ElapsedEventArgs e)
Void SendReminderEmails(System.Object, System.Timers.ElapsedEventArgs)
I know can see though using the System.Diagnostics.Debugger.Launch();
List<SessionReminder> sessions = Queries.GetSessionsToRemind();
//sessions does not have a value, method returns a list of 1
//I suspect this is a project reference issue
debugString += "sessions: \n" + sessions.ToString();
//exception thrown on this line--I suspect because sessions is null
I am using the .NET4.0 (not client profile) on all projects within my solution. Any insight on how to enable my windows service to correctly access these assemblies (webappproject.dll and extendedProcessing.dll) would be much appreciated, as these dll's are being installed by install shield.

It might not be an actual assembly reference issue. The error you are seeing is that the runtime is complaining about accessing methods/fields/properties of a null object. This indicates that your code is accessing an uninitialized object.

The exception you've displayed doesn't look like an issue with referencing assemblies. It looks like a plain ordinary bug in the code, not a missing assembly.
I always create a test harness for a windows service to enable me to run the service as a simple Windows Forms application to make debugging easy.
Separate your service implementation into a separate assembly from the assembly with the actual windows service. Create another project WPF/Winforms with a simple start/stop button. From both the windows service and winforms application reference the assembly containing the functionality that runs within your service.

Related

Copy service reference from one project to another project in same solution

We have one service reference in one project of a solution. I am trying to move this service reference to new project in same solution. But adding the service reference in new project using URL present in the service reference of old project is throwing 404 error. I am not sure where is this service hence do not have it my IIS.
I tried to copy all the files and adding them to source control. like copy paste but then it has some auto generated code under reference.cs which generates all classes. They have old namespaces and might have to change it to new namespace manually. So, not sure if this can cause some other issue as I will be manually updating the auto generated code.
Is there a way to add this service reference in my new project?
option 1 - find the url by debugging the first project
Assuming that the first project accesses the WCF service correctly, you can debug it to the point where it makes the service call and look at the the remote address in the watch
var url = currentProxy.Endpoint.Address.Uri;
option 2 - add reference to the project thats already working
add a wrapper class that makes the WCF call in your original project and then add project reference of your original project to the new project. now you can you use the newly created wrapper to access the WCF service from the new project via the wrapper

Exception occurring while connecting a C# client application to XSockets Server

I am using XSockets for having two way (web socket based) communication between an XSockets Server and an existing C# desktop client application.
I have integrated code in my existing client application for communicating with XSockets Server.
For now, XSocket Server starts as a separate console application. It starts fine with no problems.
Then I am using following pseudo code block in my C# client application:
XSocketClient client = new XSocketClient("ws://127.0.0.1:4509/MyController", "*", false); //Error occurs right on execution of this line
client.Open();
Following is the Exception snapshot that throws out;
Exception of type 'XSockets.Plugin.Framework.Exceptions.ExportException' was thrown
Custom Message: Failed to load exported interfaces in assembly Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Source: XSockets.Plugin.Framework
Stack Trace: at XSockets.Plugin.Framework.Composable.SatifyImportsExports()
at XSockets.Plugin.Framework.Composable.Initialize()
at XSockets.Plugin.Framework.Composable.GetExport[T]()
Type: System.IO.FileNotFoundException
Note: If I create a new C# windows form client project and try executing this code, it executes fine and connection is successfully made to the XSocket Server.
Means there is something disturbing from inside my existing client project.
What could the possible reason for this weird exception ? Any ideas? Thanks
Edit: I have noted that if I manually remove Microsoft.Practices.EnterpriseLibrary.Validation DLL from my Bin folder, it raises same kind of exception but with a different custom message this time. I mean then the error is related to some other DLL (of my existing application) present in Bin folder.
I ran into a similar problem yesterday and solved it like so:
Composable.ClearPluginFilters();
Composable.AddPluginFilter("XSockets.*.dll");
This code was called just before I bootstrapped my application and started my server. I know the question is kinda old but if anyone else end up here aswell.
Source from Google Groups
Ok found answer to my own raised question.
There can be two things related to this issue:
Microsoft.Practices.EnterpriseLibrary.Validation.dll was causing problem from the Bin => Release/Debug folders. From Visual studio project references list, right clicked the Microsoft.Practices.EnterpriseLibrary.Validation reference, clicked on Properties and set [Copy Local] property to false. XSocketsClient was now connecting successfully to the XSockets Server but some other logic of my existing application was still needing Enterprise Library which being Copy Local ==> false, was not coming available to the .Net Runtime, so it showed an Exception whenever Runtime was coming across those parts of code.
To resolve the later issue encountered because of removal of Enterprise Library reference, I added again Microsoft.Practices.EnterpriseLibrary.Validation reference and also added one another DLL Microsoft.Practices.EnterpriseLibrary.Common.dll and added its reference to my client application.
Now my client application connects fine to XSocket Server and also now no issue comes with the inclusion of Enterprise Library reference.
Seems like there are some things essential needed by Enterprise Library at runtime which lie in that second DLL.

How to invoke or start an asp.net web service (asmx) from another project in C#

How do I invoke or start a asp.net web service (asmx) from another project in C#. The another project can be a console application. I am not asking to consume the web service (which I am doing in Java) but how to start the web service in C# but from a different project.
I already tried including the webservice project in my console application project and also added a reference to the webservice project, included the namespace but I get an error,
The type 'System.Web.Services.WebService' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
I am trying to invoke the webservice by creating an instant of the webservice class which I guess is already a wrong way.
Thanks in advance!
You are going to want to add a web reference.
How to: Add a Reference to a Web Service
Then you are going to want to set multiple projects to start. This will allow you to debug both.
How to: Set Multiple Startup Projects

WcfSvcHost throws LoaderException when attaching to IIS7 process, but everything seems to work?

I have been implementing RSA security for a project I am working on.
I'm using the SecurID4Net files found on the web to get this rolling, which by default are targeted to the .Net 2.0 framework, ANY CPU.
I created a derived SqlMembershipProvider which references the SecurID4Net.Interfaces project, targeting .Net 4.0, any CPU.
My class lib "Services" references the SqlMembershipProvider, also targeting Any CPU.
My web app "Services.Web" references the "Services" lib, .Net 4.0, Any CPU.
I'm not using the client profile for any assembly targeting .Net 4. Every reference I have described here has Copy Local set to True.
I have my local IIS default web site set up to my output folder for the web project, so I can Ctrl-Shift-B and browse my IIS folder. When my client app signs in for the first time (possibly 2 or 3 times), it works fine, but after that sign ins fail. Additionally, when attaching to the IIS7 process (Services.Web), I would get the following error:
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.Assembly.GetTypes()
at Microsoft.Tools.SvcHost.ServiceHostHelper.LoadServiceAssembly(String svcAssemblyPath)
The DLL it is having problems loading is the SqlMembershipProvider I created.
So, I changed the SecurID4Net.Interfaces project to target .Net 4.0, and recompiled. It seems to work consistently now (still testing this part), but CTRL-ALT-P to attach to the IIS process still results in the WcfSvcHost error popping up before I can actually attach to the process... everything else seems to be working.
I'm running my VS as admin so I can attach to an IIS process;
All references described are set to Copy Local = true;
All assemblies are targeting .Net 4.0 Full Profile;
All assemblies are reachable, not blocked by the copy operation to the server where this is hosted;
To my knowledge no syntax issues with the web.config.
Anyone have any ideas why this error keeps popping up? Why would this error pop up when everything seems to be working?
I think I found the answer almost immediately. After inspecting each AssemblyInfo.cs file in the chain of projects, I found this in my SqlMembershipProvider assembly:
[assembly: AssemblyCulture("EN-us")]
I removed the value so it's an empty string.
I had a very similar issue in another project I worked on a few months ago where the web site had this filled in, and the web site worked on the very first load, but every load thereafter failed with a very cryptic error which, after drilling down, found that it could not load the assembly, giving a FileNotFoundException.
I don't know why this tiny, simple attribute would cause so many headaches...
EDIT: I'm 99% certain this was it. When I attach to my IIS process I no longer get a WcfSvcHost error.

Building .NET COMInterop project without first registering the COM service

I am building a C# UI to interact with a COM Service (.exe). The VS2005 COM project outputs a valid typelib (TLB), which is referenced by the C# project. However, the VS2005 C# project insists that the service be registered in order to build the C# project, even though the typelib appears perfectly valid when examined with the OLE Object Viewer.
We would rather not register the service on the build server, if possible. Further, debug builds cannot register the COM object as a service, since this makes debugging in general, and startup code in particular, more difficult (can't use F5 - Start Debugging).
What should I look for in order to get this working? Do I need to register just the TypeLib? If so, why would COMInterop insist on having the service or TLB registered in order to build correctly (and, is there a command-line tool for this on Win2003 with VS2005)?
If I recall correctly, once apon a time I built a C# web service which referenced the Win2003 firewall COM object, but we built it on Win2000 (which does not have such a thing) simply by referencing the TLB file with no registration required, so I don't understand why this should be a problem now.
You need to create interop assembly using tlbimp.exe tool. And add reference to this assembly in your project instead of typelib. In this case building will not require registered com server.
If you do not want to ship yet another assembly, you can extract interface description from generated interop assembly using Reflector and include them into project as source files.
Solved this by adding a -regtypelib command to the COM service, which calls the AtlRegisterTypeLib function but does not register the service. Suboptimal because it will leave registery entries on the build server, but quick and it works.

Categories

Resources