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

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.

Related

BizTalk 2010 map scripting complex external assembly throws assembly not found

Scenario: Integrating external web application with secure information. Web site calls BizTalk WCF service with one (or more) fields that have been encrypted with web site's encryption class. Inside the publish map I need to convert the web encryption to BizTalk environment encryption (yes, the differences are necessary). This used to be done in the SOAP asmx web service data types code but now needs to be done either in the map or pipeline. Map seemed easiest to implement with scripting functoid reference to external assembly where I call decryptor for the one and re-encrypt with the other pretty much exactly like I did in old web service.
Problem: External assembly is relatively simple in that it consists of one class with default constructor no arguments, with one public method the takes a string and returns a string. Input is web encrypted valve, output is my encryption value. I reference 3 custom libraries for this encryption swap inside the method. None of the referenced assemblies are being called from the scripting functoid. All are added as references in map project. All are strong-named. All are GAC'd.
Yet, when I test the map it gives the following error:
Function 'ScriptNS0:Myfunction()' has failed. Exception has been thrown by the target of an invocation. Could not load file or assembly 'OurCompany.Project.WebEncryption, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123654789abcd' or one of its dependencies. The system cannot find the file specified.
I double checked versions etc in the GAC_MSIL folder and everything matches. What gives?
ARRRGGGG! Curse you Visual Studio! VS was not picking up the changes to the GAC and additionally I don't believe it liked my original dotted namespace/assembly name. I simplified to MapScripting for NS/Asm and restarted VS and it worked. UGH!

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

Strong name required for assemblies referenced from unsigned project?

I'm working on a WCF service. The WCF service interface and implementation are in separate projects. There's also a Windows service project to host the WCF service.
One of the things the WCF service implementation needs to do is call out to several external (SOAP) web services. The way we typically structure this is we create a separate class library project for the SOAP service(s); we'll create a Web reference and a factory/helper method in the class library.
The above detail may or may not be pertinent to the real problem. Which is that I get an error when building the WCF service implementation project (where X is one of the SOAP service wrapper assemblies):
referenced assembly 'X' does not have a strong name
But the WCF service implementation project isn't set to be signed (nor is the interface project). And at this point the only two things referencing it are the Windows service project and a unit test project -- and neither of those is signed either.
The WCF implementation also references other (pre-existing) web service wrapper projects, but it's only complaining about these two. I've pulled up an existing and a new project file in a text editor side by side... And I can's see any significant differences.
I've also checked whether any of the projects is importing a setting that requires it to be signed, as described in Stack Overflow question Remove signing from an assembly. That doesn't appear to be the case.
I was attempting to use AutoMapper -- 1.1 since we're still on .NET 3.5 -- in my WCF implementation. That is a signed assembly, so I can see where it might have a problem reflecting on my code and the SOAP service wrappers. But it seems to me that would be a run-time problem, not build time. But because I suspected it might be at least a contributing factor, I removed AutoMapper and the dependent code but I still get the same error.
I have researched the issue, most of the search results consist of instructions on how to sign (possibly third-party) assemblies.
I've tried removing and re-adding the references, restarted Visual Studio and my PC.
Visual Studio 2010 / .NET 3.5 on Windows 7 64-bit.
I'm sure I'm missing something fairly obvious ... I just can't figure out what.
Well ... this is embarrassing.
As I suspected, it was something simple. The service interface and implementation were in fact set to be signed; I was incorrect about that. However, this was done not in the project settings/file, but in the AssemblyInfo.cs file, via the directive
[assembly: AssemblyKeyFile("keyfile.snk")].
I was not familiar with this method of signing. I gather it's been deprecated since VS2005 -- partially because it's more straightforward to manage signing via the project properties, partially because putting this information into the AssemblyInfo is considered a security risk.
And I had in fact looked at at least one of the AssemblyInfo files ... but apparently I didn't scroll down far enough. (I said it was embarrassing.)
Hopefully someone else can benefit from this.
You can not reference an unsigned assembly from a signed assembly, the project which fails to build should be the one that has a strong name key tied to it.
It is possible that you have a signed version of one of your references in the Global Assembly Cache that MSBuild is picking up instead of the reference you selected.

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

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