Ambiguous references with linked .CS File and WCF Service - c#

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.

Related

Use other DataContract classes than the generated one's

Our customer sent us a WSDL file for their web service API. We used Visual Studio's "Add Service Reference" to consume it.
However: we only want to generate a service proxy, not the data contract classes. We want to reuse classes we already have within a referenced assembly.
How to instruct "Add Service Reference" to not create Data Contract classes?
...or how to instruct WCF to use our Data Contract classes from the referenced assembly instead?
This comes down to the following options on the Advanced... part of "Add Service Reference":
which also map to the /r / /reference option in svcutil.exe.
This defaults to enabled, so if it isn't working: there's a good chance your types are not exact matches. The easiest way to ensure exact matches is to reference the same library dll from both projects, or worst case the same .cs file (a dll reference would be preferable, IMO). The data-contract namespace and name are the usual culprits here, and must match exactly (as must the members).
Add your data contract classes (or even better create a new library for you data contract classes and reuse both in the service and client side) to the project including your service references. When adding a new service reference or updating an existing one, select "Reuse Types in Referenced Assemblies".
This is old but I have one doubt about this.
I can access classes when adding service reference, this is ok
I can't see these classes when working with all projects inside the same solution in Visual Studio and adding "project" references.
Is there a way to test client side code using all projects inside the same solution in Visual Studio?
thanks

How can I consume A web-service reference like a dll

I have a small question: Can we consume a web-service reference like a sample dll?
I mean something like following: 1. Add reference to assembly in the references 2. add namespace to using (using mywebservice) 3. use it in code like:
var service = new mywebservice.Service1();
var result = service.GetSomething()?
Why I'm asking? It's because of I tried but I get a "strange" error: Cannot load assembly "MyService.dll version, and so on". Thanks in advance!
You can create a service reference
http://msdn.microsoft.com/en-us/library/bb628649.aspx
What you should note is what the name space is that you put your proxy under. It's the namespace that decide the what you should type instead of "mywebservice"
Yes, you can perfectly create a WCF Service Reference (aka a client proxy) and stick that in an assembly purely meant to serve as a centralized, shareable client proxy. This way, when adding a service operation, you don't have to update the reference in all projects that consume your service, but just update the reference in the assembly.
Please note that if the service reference was configured to reuses types found in assemblies referenced by the client proxy project, you also need to add references to those assemblies to the project using your client proxy assembly.
Cannot load assembly "MyService.dll version, and so on"
That is a symptom, usually explained by errors surrounding that error. It could prove really useful if you showed all relevant warnings and errors in your question.
You probably just need to add a reference to System.ServiceModel to the project using your client proxy assembly, are missing other assembly references or your service project fails to build so it acutally can't find the DLL.

Can't use service reference in ASP.NET project

I successfully added a WCF Service Reference to my ASP.NET project, but I'm not able to use it.
I can't import the service referenece via using MyProject.MyService.
This worked in a standard Windows Form project and I now, that the requested service is working.
Thanks for your help.
Edit:
There occure Warnings during the generation of the service client
I think the "main" warning is the following
Warning 1 Custom tool warning: Cannot import wsdl:portType
Detail: An exception was thrown while running a WSDL import extension:System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
This warning comes only at the generation in an ASP.NET project. The generation works in my other projects.
It seems that this is a problem of a class I'm not responsible for. May I have to change the settings for the service reference creation or avoid some imports in the original WCF Service class?
We need some more information in order to troubleshoot this for you. Is the reference being added but you cannot use it?
I would suggest looking at the reference.cs file (under the service reference, you may need to tell solution explorer to show all files - little icon of the folders and files at the top) and seeing what namespace it has been generated with.
I think you're probably just not using the correct namespace to reference the client.
The other possibility: check for any compiler warnings, they might give you a clue as to what is going on.
EDIT: This seems to be an issue encountered by others. Do you have the json.net package installed?
Please see the following for details:
Getting "Recursive collection data contract" when referencing a WCF service with a slightly complex method

Sharing classes between multiple WCF services

I have two WCF services, one for reading operations and the other one for writing operations.
Although they are doing different operations, both of them are sharing the same classes (through a DLL reference to other project wich supports the model of the problem).
My problem is that, when I try to add both services to my C# project I can't share the same namespace:
Cannot create a service reference with namespace 'X' because the name
is already in use by an existing service reference, folder or file
If services are placed in different namespaces, classes are different and duplicated code grows a lot.
Is there any solution?
Edition: Very similar to: Two WCF services with different contracts but same business objects . No solution anyway.
If you use VS 2010 (probably works on 2008 also) click advanced button in "add service reference" window and use "reuse types in specified referenced assemblies". You may have to move generated types to other assembly depending on how your project look like.

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"

Categories