How to add service as reference to class library? - c#

I have an existing WCF Service. I want to add it as a reference to a class library whose target framework is .net6, so that I can call WCF service methods I cannot see any option to add the service as a reference to the class library.
I am using Visual Studio 2022.
Is there any way to do this?

#madmonk46 comment helped me in adding the service as a reference.
So you need to right-click the project in Solution explorer, then click on Add then Service Reference.
Then you can follow the instructions which are mentioned here. you can ignore the first point.

Related

Cannot instantiate objectVO class from another project from WCF Service Library project even though they are in the same solution

I've been trying to create a webService on a .NET platform using Visual Studio's WCF Service Library project template. I noticed that when I tried to use a VO object from another project file within the same solution... programatically, the compiler has no issues and runs fine... but when I call the webService, it will stop the application prematurely.
I've done some google-ing and the closest answer I could find was the possibility that since WCF is run from a separate location, the VO.dll file that I added in as a reference is not available to it.
As a reference to the above statement... please find the enclosed link:
http://forums.asp.net/t/712261.aspx
Thanks

Unable to instantiate service in client app

For some reason I cannot instantiate the class nor access the namespace of WCF service references I add to my VS2010 project. I had this problem last week too and I solved it by creating a new client project, however I don't want to do that every other day when I need to add a service reference.
Does anyone have any experience with this kind of problem?
I found my solution at Sometimes adding a WCF Service Reference generates an empty reference.cs
I chose to configure the service reference and unchecked "Reuse types in referenced assemblies", that solved it!

Problem referencing .Web project in Silverlight project

I'm trying to reference my domain service by following this documentation.
The following two declarations work fine
xmlns:riaControls = "clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.DomainServices"
and
xmlns:data = "clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data"
but I'm having problems with this one
xmlns:domain="clr-namespace:SNMPApplication.Web"
This error appears, even after I rebuild the whole solution:
Undefined CLR namespace. The
'clr-namespace' URI refers to a
namespace that is not included in the
assembly.
I've tried adding a reference to my Silverlight project but I get this error
You can only add project references to
other Silverlight projects in the
solution.
Does anyone have an ideia of what the problem is? :/
Thank you so much in advance.
The way you add a reference to a RIA DomainService is not through the traditional Add Reference dialog. You do it through the project settings. In the Silverlight Application, goto the project settings, specifically the 'Silverlight' tab. On that tab will be a 'WCF RIA Services link'. You'll be able to select your project that contains the DomainService.
After you select the project, Visual Studio will generate code for your Silverlight application. This is found in a Generated_Code folder (which isn't visible in the project, though you can see it in the folder). This is where your code that you need to reference will be.
If you genuinely need to access the Web project from your SNMPApplication project, then I think what you'll need is either of the answers to this question: How to access web application class into silverlight application
You are not actually creating a reference to your web project when you do this, it resolves it at compiletime to create the generated g.cs files (I suspect), This is one of those magic things that does stuff in the background ( I think it stores it in your project file and you set it in the project properties --> silverlight tab under WCF Ria Services link).
Please check the following:
Your SNMPApplication.Web is in the same solution as your Silverlight project (SNMPApplication)
and that your reference is the same as your DomainService class namespace you added to your web.Project class (It might be in a sub folder and that is why it's not working,
If you haven't created a domain service please add one as this is what silverlight and ria services uses to connect between your entity model and silverlight. (Have a look here from Adding a DomainService class
xmlns:domain="clr-namespace:SNMPApplication.Web"

WP7, How to use a service reference after adding it to Visual Studio 2010

I'm following this example for connecting to the Bing Maps geocode service:
Link
About half way down the page, it explains how to add a service reference in Visual Studio 2010, which I was able to do successfully. Then it says to add "using GeoCode.GeoCodeService", but when I do, I get an error saying "The type or namespace 'GeoCode' could not be found"
Am I doing something wrong. The steps are pretty simple and nothing gave an error. What else do I need to do in order to access the service?
After you added a Reference using your WSDL URL:
Under Solution, under Service References, right click on your Service Reference folder that you want to reference (example: com.gold.services.description1)
Select View in Object Browser.
You will see the class name in the Object Browser window that will open up.
(example: GoldWeb.com.gold.services.description1)
Copy and paste the class name into your code.
Example:
using GoldWeb.com.gold.services.description1;
Then you are ready to start using its classes.
When you added the service reference, you gave it a class name. Have a look in your solution explorer to see what you called it, and then you have to instantiate that class to use the service.
I seen this happen, when a service may be added to one project and is being back referenced in another project. For instance, the main project has the service reference, but library code, that's is reference in the main project, tries to instantiate the webservice. This is a circular/backreference and you can't add the namespace of the main project back into the library project, since the library project is already reference into the main project. I know this seem obvious, but in really big projects, it's easy to get lost to where you're at in code.

Ambiguous references with linked .CS File and WCF Service

For brevity purposes, this post relates to ambiguous references in a Silverlight Page.XAML.CS file, whose project contains a service reference to a WCF service and a MyClass.cs file added as a 'link'. The Solution contains the Silverlight Project and a Web Project that contains a WCF service and a MyClass.cs file (along with the aspx files etc).
For some reason I am getting ambiguous reference errors once I add the service reference to Page.xaml.cs. Prior to adding the using statement for the service ref, I had one for MyClass.cs (which remember had been added to the SL project as a link) to the page, and it was running fine. Once the SVC ref is added, the compiler complains about ambiguity in my call to any class / property in 'MyClass.cs, such that a reference to MyClass.Class becomes ambiguous to 'ServiceReference.MyClass.Class...Seems very strange to me.
Assumptions & Clarifications
I ensured that no namespaces, class names, methods or variables had similar names
WCF service must reside in web application to have access to other non Silverlight assemblies etc
Other .cs files in the Silverlight project reference MyClass.cs, otherwise I would have simply removed the link to MyClass.cs, and allowed referenced MyClass.cs through the service ref.
My assumption here is that this has something to do with adding a file as a link? Any KungFu Masters out there able to offer some insight as to why this is occurring, alternatives to adding as a linked file, other ideas?
Is MyClass a class used by the service for which you added a Service Reference? If true, then there are two versions of each class in MyClass.cs: one from MyClass.cs, and one from the service reference.
You should pick one or the other - either use the service or don't use the service.
If you've added "MyClass.cs" as a link in both your Silverlight and Web projects then it will most likely cause a name collision, fortunately they should be in seperate namespaces. The linked class will be in the original namespace and the one generated by the Service Reference will be in the generated namespace.
You can use the "Reuse Types in Referenced Assemblies" option when generating the Service Reference so that the generated service proxy uses your linked class rather than generating a new one. There are however a couple of tricks to get this to work correctly, I outlined these in a post a few months ago Resuing types in Silverlight Service References.
I hope this helps.
Nigel Sampsons Answer led me in the right direction, thanks a million!
The Solution
Create a new project for the class file, and add a reference to that project in your Silverlight Project. Then when you ad your service reference and select "Reuse Types in Referenced Assemblies", it will not generate it's own implementation of the class, eliminating the ambiguous reference.
Unlike a typical client / server service scenario the class file must be compiled seperately for Siverlight and ASP.NET.
I've been able to overcome this issue by simply un-checking the re=checking
"Reuse types in referenced assemblies" when I configure the service reference. I was getting ambiguous references between the Class Library and the service reference which resulted in what I refer to as a cascade of errors, growing with every move I made.

Categories