WCF 'sub main' not found - vb.net

I must be losing my mind...
After getting a test WCF hosted in a windows service, I'm trying for another one (practice, practice, practice).
I created a WCF service library, added one function. Then created a Windows Service, and added my WCF to the project. Did the rest of the stuff located here (http://joefreeman.co.uk/blog/2010/03/creating-a-setup-project-for-a-windows-wcf-service-with-visual-studio/)
Now I'm getting this "Sub Mian was not found in [WCF app]" error when I try to build the solution.
I didn't think WCF projects required a Sub Main as they are services and not applications. What am I doing wrong? I didn't have a sub main in my last project. Any ideas?

For others who are looking for the same answer I was...
Created a service in VB.NET (VS2010), and was getting the same error listed above...
'Sub Main' was not found in 'SERVICENAME.Service1'.
In VB.NET, VS2010 created a "Service1.Designer.vb" file for me automatically. Inside of that file was:
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New SERVICENAME}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
To resolve this, right-click your solution name in the solution explorer, go to the "Application" tab, and pick "Sub Main" as your Startup object in the "Startup object" drop-down box. Recompile.
Granted, the file names and structure will be different in C#, but the idea is that Visual Studio put the Main procedure SOMEWHERE, and it hopefully should already know where it is.

The WCF service itself doesn't need a Sub Main, but your Windows Service does.
Compare against your previously built, working Windows Service.
edit: using C#, when I create a new project using the Windows Service template, I get the following initial code in a Program.cs file (static void Main is your Sub Main). I'd be very surprised if you don't also have this in your Windows Service.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()

Check you haven't accidentally changed the Application type to something stupid like "windows forms app", hit the properties of the project and make sure it's on "class library".

Related

Using a Service Reference in VB.NET

i have added a service ref in my VB.NET application.
i can see the objects in the object browser. i need to log in first to obtain a session ID.
when I try
dim client as new serviceReference1.IGPSBulkData
I get an error
'New cannot be used on an interface'.
IGPSBulkdata is the only option that includes the login function so Im not sure how to make this call
any ideas?
When you add a service reference several classes are being generated from the service' WSDL. Take a closer look at the generated code 🤓 There will be something like GPSBulkDataClient.
This generated client class can be used to communicate with the service.
The error is correct; you cannot instantiate an Interface.
If you open Object Browser in Visual Studio and search for IGPSBulkData you should be presented with a list of classes which implement it. From there you can instantiate your client object (if that's what you need to do with that class).
So if there was a class called GPSBulkDataThing which implements IGPSBulkData your code might resemble:
Dim client as serviceReference1.IGPSBulkData = new GPSBulkDataThing(maybe with some arguments here)

Running function of one application from within another application

I've got two standalone applications:
First one:
Namespace FirstApplication
Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Public Function RunBatch(Parameter as String) as Double
'Do some work
Return SomeValue
End Function
End Class
End Namespace
Second application:
Namespace SecondApplication
Class MainWindow
Public Sub New()
InitializeComponent()
End Sub
Public Sub RunBatch()
'Call RunBatch() from first Application, get show the result
Msgbox(RunBatch)
End Function
End Class
End Namespace
Both are WPF, .Net 4.0 based. The goal is to have second application call on the first one and execute a function in it.
The key part is that both applications are used primarily independently and only occasionally second calls on the first. Because both applications need to exist as executable, I don't want to solve the problem by creating a dll of the first application - I would need to maintain both the executable and dll update to date with potentially disastrous consequences if they fall out of sync.
So the question is whether it's possible, to create an instance of first application within AppDomain of the second one and, crucially, execute functions of that instance.
I don't believe you can create one AppDomain inside of another.
Any other options you have (using WCF, or old style .NET Remoting, or crossing AppDomains) are all going to be more complicated than just making a single DLL that both applications can reference. So long as you don't change the assembly number of the shared DLL you won't have to recompile each exe if you make changes to the DLL (assuming you don't make breaking changes like changing method signatures).
Does FirstApplication have to do something to SecondApplication? Are you trying to control a feature of one application from another? If so you will need something like WCF (using Named Pipes or just a self-hosted web service). Or just trying to not have to write the same code twice? Then the simplest approach is probably to create a single DLL both applications reference.
Apparently, this can be done via reflection. The process is straightforward, though not nearly as convenient as using dll.
Public Class CalltoExternallApp
'this is the declaration of the external application you want to run within your application
Dim newAssembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom("Mydirectory\Myfile.exe")
Public Sub Main()
'Loads the main entry point of the application i.e. calls default constructor and assigns handle for it to MyApplication
Dim MyApplication = newAssembly.CreateInstance("MyApplication.RootClass")
'Get the type which will allow for calls to methods within application
Dim MyApplicationType as Type = newAssembly.GetType("MyApplication.RootClass")
'If calling a function, the call will return value as normal.
Dim Result As Object = LunaMain.InvokeMember("MyFunction", Reflection.BindingFlags.InvokeMethod, Nothing, MyApplication, MyParameters)
End Sub
End Class
Check also here for adding the event handlers to instances created via Reflection:
http://msdn.microsoft.com/en-us/library/ms228976.aspx

Type being passed through web service not being recognized by consuming code

I am creating an XML web service that passes an array of custom types. In my consuming code I am referencing the code as a web reference which I have given the namespace MYWS. Now in code I am trying to assign the results of my web service call to an array of my type like so :
'instance to make a call to my web service
Dim srv As New MYDWS.ServiceNameWSSoapClient
'array to hold the results
Dim arr() As MyClass
'assign the web service call results
arr = srv.myWebMethod()
When I do this the complier complains, saying:
Value of 1 dimensional array of my.namespace.MyClass cannot be
converted to 1 dimensional array of my.namespace.MYWS.MyClass because
my.namespace.MYSW.MyClass is not derived from my.namespace.MyClass
Now I understand the message, the thing is they are the same class. The class is declared in my calling code by the web service references a dll from that project. How do I tell the compiler that these are the same type? Any help would be very much appreciated. Thanks!
The upshot is that you have a namespace mismatch. If you right-click on MyClass in your example and select Go To Definition, where does it take you? I suspect that you may end up in a locally defined class.
The solution is to change
Dim arr() As MyClass
to
Dim arr() As MYWS.MyClass
Update based on information in comments
The problem with using the web service is that you cannot cast it to a local class.
You have a couple of options depending on exactly what you need out of the local class.
If you only need methods to act on the data in the class or you need additional properties, you can create a partial class in your environment that extends the class created by the web service. For example:
Namespace MYWS
Public Partial Class MyClass
Public Property SomeAdditionalData As String
Public Sub SomeMethod
' Perform some operations on the class members
End Sub
End Class
End Namespace
However, if you have calculations or other work embedded in the class, then you will need to get the data using the web service class, then copy the data from that class into your local class. If the properties have the same names, you could ease this task using reflection.
As another option, if you have control over the web service, you could change it to a WCF service. This will allow you to reuse the exact same class code on both ends of the communication pipe.
Found a solution to the problem. In the web.config I found this:
<add key="net.mydom.mydom" value="http://localhost:7452/dir/mysvc.asmx"/>
which was what the system automatically entered when I registered the web service. I got the error messages on screen, but everything compiled and ran w/o problem.
When I manually changed to this:
<add key="net.mydom" value="http://localhost:7452/dir/mysvc.asmx"/>
The error messages went away and everything continued to function as expected.
(That only took my 7 years to figure out...)
UPDATE:
Well, not quite the fix, but it must be close. After awhile, the problem came back, when I switched back to to:
<add key="net.mydom.mydom" value="http://localhost:7452/dir/mysvc.asmx"/>
it went away again...sure to come back at any time...
UPDATE
If I explicitly add:
imports net.mydom
to the top of my code, the message goes away again (even though I was explicitly using the full net.mydom. when typing the variables.

Reference Windows Form Project From another Windows Form Project in same Solution

I have a solution with several projects most of which are code or control libraries. I have a main windows forms application that references and uses these libraries. What i am trying to do is create a 2nd windows application that extends the main one, but i would like to be able to deploy them as separate exe's.
When i try to add a reference to the new app referencing the main app; all seems fine until i try to run the new app i get several error msgs similar to below:
Error 1 Could not find file 'ADODB.dll' referenced by assembly 'D:\Visual Studio 2005\Projects\X\XX\bin\Debug\XXX.exe.manifest'. <newAppName>
i have tried adding references to all the dll's in the error messages and they are still listed when i try to run the new app. I thought of a few work arounds but they require user changes to maintain separate exe's at deployment. I would like to avoid this if possible. Any ideas?
Thanks in advance, Jeff
Your windows forms applications should not be the point that you extend, the exe files should really just be a shell for launching your process (as much as possible anyways). So this response doesn't answer your specific problem of reference exes as this is not considered good practice.
All the extensions should be made to your code or control libraries off a known interface or contract. Generally the process for extending applications like this is to use alternate or additional DLLs which are loaded at runtime.
Say you have an application called clock which is to display the time.
You can structure your application with a set of contracts (or interfaces) in a referenceable DLL "Clock.Contracts.dll":
public interface ITimeService
{
public string Name { get; }
public Date GetTime();
}
You then have each implementation of this in another DLL ("Clock.LocalComputer.dll", "Clock.InternetTime.dll"
public class LocalTime : ITimeService
{
public string Name
{ get { return "Local Time"; }}
public Date GetTime()
{ return Date.Now; }
}
In the UI/EXE you always reference the interface don't call the implementation.
How do you get an instance of the implementing class, using Reflection to identify if a class in a DLL implements the interface and Activator.CreateInstance to generate the class.
http://gsraj.tripod.com/dotnet/reflection.html
There are patterns like Inversion of Control and Dependency Injection which help to address these things in a standardized way in your application. 3rd party libraries like Castle Windsor, Spring can assist. A google search on these will give you some reading material.
I will say that it can take a while to fully get your head around these things.
ok i found a reasonable work around. Basically you add all the reused forms as existing items, but instead of just clicking add you click the drop down arrow and choose add as link.
It would be great to redesign as JTew suggested above but this gets me where i need to be without having to move code.
You can find more information here
Thanks for all your time looking this over and hopefully is helpful to more
Jeff Spo

How do I properly set up a Silverlight-Enabled WCF Service?

EDIT: I started a closed vote on this question because I resolved the issue. I was doing everything fine, but a reference to an ASP URL rewriter that I downloaded and uninstalled a while ago still had a reference in IIS. This forum post by Waclaw Stypula (the one with the steps) helped me track this down, by accident. When I launched the run command, IIS told me that it (obviously) could not find the rewriter DLL. I removed the reference and the app ran fine after that.
I am following the silverlight.net tutorials by Jesse Liberty. Currently I am trying to do tutorial three, but I am running into a wall under the heading "CREATE THE WEB SERVICE" (about halfway down).
First, when I create the new service by adding it to the solution, the tutorial indicates that three files should be created; IService1.vb, Service1.svc, and Service1.svc.vb. I do not get the IService1.vb file when I add the service to the solution. I downloaded the copy of the project they provided, and the Service1.svc.vb file is in there, so I added one manually and copied the contents of the file. The tutorial says it is a VB tutorial, but displays C# in the accompanying screenshot so maybe that is the issue.
After I get all the files made up like the tutorials (copy/paste to make sure I don't have a typo), I try to add the service reference and get the following error:
The service class of type KeyboardControl_Web.Service1 both defines a ServiceContract and inherits a ServiceContract from type KeyboardControl_Web.IService1. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Consider moving the ServiceContractAttribute on type KeyboardControl_Web.IService1 to a separate interface that type KeyboardControl_Web.IService1 implements.
I tried googling different portions of the message, but did not find much useful information.
Here is the code for the different files:
//IService1.vb
Imports System.ServiceModel
' NOTE: If you change the class name "IService1" here, you must also update
' the reference to "IService1" in Web.config.
<ServiceContract()> _
Public Interface IService1
<OperationContract()> _
Function GetAllLocations() As List(Of Address)
End Interface
//Service1.svc.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
' NOTE: If you change the class name "Service1" here, you must also
' update the reference to "Service1" in Web.config and in the
' associated .svc file.
Public Class Service1
Implements IService1
Public Function GetAllLocations() As List(Of Address) Implements IService1.GetAllLocations
Dim db As New DataClasses1DataContext()
Dim matchingCustomers = From cust In db.userControlDemos Select cust
'Return matchingCustomers.ToList()
End Function
End Class
I am new to Silverlight/WCF in general, as well as to Interfaces and Services. Can you guys help me get on the right track?
EDIT: I should add that I am using Visual Studio 2008, on Windows Vista Business SP1.
if you have the Silverlight Tools for Visual Studio installed, instead of using the default WCF template, consider using the "Silverlight-enabled WCF Service" which simplifies things for you and puts all the required configuration in place. This new template was introduced in Beta 2 and still exist (ref: http://timheuer.com/blog/archive/2008/06/06/changes-to-accessing-services-in-silverlight-2-beta-2.aspx).