WCF: Should all datacontracts use the same namespace? - apache-flex

I have several service contracts exposed over WCF, which use multiple datacontracts. The service is to be consumed by Adobe Flex. I've run into many problems getting Flex to consume the WSDL produced by the server. I used the procedure outlined here to squeeze the WSDL into a single file (I used FlatWsdl and not WcfExtras, if it matters).
Now I'm getting a bunch of errors when using VS2010's own wsdl.exe tool to extract my metadata. Specifically, I'm getting the following errors:
1) Schema validation warning: Schema item 'complexType' named
'ArrayOfKeyValueOfSomeKeySomeValuep1alXzIb' from namespace
'http://schemas.microsoft.com/2003/10/Serialization/Arrays' is invalid.
Namespace 'http://My/Wcf/Namespace' is not available to be referenced
in this schema.
2) Schema validation warning: Schema item 'complexType' named 'MyComplexType'
from namespace 'http://My/Wcf/Namespace' is invalid. Namespace
'http://schemas.datacontract.org/2004/07/My.Real.Namespace' is not available
to be referenced in this schema.
Now, the Dictionary ("ArrayOfKeyValueOf...") type is apparently defined in an external schema, which is not imported by my WSDL. Please note that SomeKey and SomeValue have a datacontract namespace of My/Wcf/Namespace.
As for MyComplexType, the weird thing is that if I change its Datacontract-namespace to the same namespace used by my services, servers, and bindings, then error (2) goes away. I can't figure out why.
What I'm asking boils down to two questions:
1) How can I add an import directive of an external schema to my WSDL? I saw some solutions on-line that suggested writing a wrapper class around the containers. This is something I would really like to avoid.
2) Do I really have to put all my datacontracts into a single namespace? I don't have any technical objection to doing it, but it's going to be very cumbersome to modify the namespace of all datacontracts. Perhaps there's an automated way of achieving this?
TIA!

So just in case anyone else runs into the same problem with accessing a WCF (BasicHttpBinding) server from a Flex client:
1) No, all DataContracts need not be in the same namespace (but all ServiceContracts, service implementations, and bindings do have to be in the same namespace!).
2) The crux of the problem is that Flex expects each xsd:schema to have an "xsd:import namespace" for all the namespaces it references. This import should be of the form:
<xsd:import namespace="http://schemas.datacontract.org/2004/07/SomeNamespace" />
There doesn't seem to be a way to do this with WCF, since it automatically adds an auto-generated schemaLocation attribute to this tag, which makes Flex try to fetch this schemaLocation. The only workaround appears to be manually manipulating the WSDL at the XML level (e.g. by exposing a REST endpoint for retrieving the metadata).
Thanks to Yaron Naveh in the MSDN WCF forums for his help. The discussion can be found at http://social.msdn.microsoft.com/Forums/en/wcf/thread/b9429e30-e4d5-454f-9fbd-bae39990ff33.

Related

LINQ to SQL Connection Strings with class library projects

LINQ to SQL Connection Strings with class library projects
By default, creating a new LINQ to SQL model (.dbml) will put the
connection strings in both the application settings file and also
web.config / app.config. This is not so much of a problem for web
projects, but what about class library projects? i have a connection
class where I can use it to check connection in all pages but I have an
error where it cant read DataContext at all.
This is a photo that shows my problem.
Generally speaking, class libraries don't support config files. There are ways to make it work, but it's not considered a good practice since different applications may use the same library to interact with different instances of the database. I would recommend looking at a dependency injection or inversion of control solution like Ninject to pass the connection string to the constructor from the app that references the library.
UPDATE:
If you absolutely must read a config file from an assembly instead of the calling application, it can be done with ConfigurationManager.OpenExeConfiguration(). There are several answers here on SO that provide code samples for doing so, but I'm not going to link to them because I strongly encourage you not to go down that road.
By the looks of it you're not using LINQ to SQL - all I can see is an EntityFramework edmx. Check your code generation strategy, and make sure you're trying to instantiate the correct context name (think it's whatever the Entity Container Name is set to).
Also you need to make sure System.Configuration is referenced.
You need to put the connection strings in the main application app.config
Just put a copy of the connection strings in there and you can access them or in this case web.config

System.Drawing namespace can't be used in Web Service

I've put together a c# console app that runs a web service intending to download a simple pdf document. I can return the object however I can't seem to be able to figure out how to save the pdf object to file.
ServiceName.ImageResponse responseObject = Response.GetDocument(12345);
MemoryStream ms = new MemoryStream(responseObject.Document);
//System.Drawing is the problem (namespace can't be used in a Web Service)
System.Drawing.Image Img = System.Drawing.Image.FromStream(ms);
Img.Save("c:\\saved.JPG");
This is not compiling as I'm receiving (a red squiqly line under System.Drawing and) the following error:
Error 1 The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)
http://msdn.microsoft.com/en-us/library/system.drawing.aspx states the following:
Caution noteCaution
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.
I would simply like to know if there is another way. I can't seem to figure it out.
trebuchet is probably correct. Check to make sure that you have actually done the work of adding a reference to the required dll. Namespaces do not have a 1 to 1 correspondence to .dll files, for what it is worth.
As to this:
Classes within the System.Drawing namespace are not supported for use
within a Windows or ASP.NET service. Attempting to use these classes
from within one of these application types may produce unexpected
problems, such as diminished service performance and run-time
exceptions. For a supported alternative, see Windows Imaging
Components.
I don't know for certain, but my guess is that several of the components within the System.Drawing namespace expect the process to have access to a window station, the desktop, etc., which is where you would see run-time exceptions. I'd take a punt and see if it works (once you've fixed your compilation error). What they're really saying here is "don't call us if this breaks" :)

ASP.NET with Unity 2.0

Can anyone help point me to some good resources for working with Unity 2.0 in an ASP.NET that doesn't talk about ASP.NET MVC?!?
We are not using MVC and I am struggling to get Unity to inject dependencies into my pages following the couple of examples I've read (which are all based on David Hayden's work so they are all presenting the same examples and code).
UPDATE
I tried to go the PageHandlerFactory route but the example (here) is incomplete and no source code is available to accompany the article.
So, I decided to try the custom HttpModule approach described here and here. Again, no source code is available beyond what is shown so it is difficult to troubleshoot issues.
The problem I have now is that all of the plumbing appears to be wiring up correctly but the Buildup method does nothing to my page. I can see all of the types are registered in the container when I set a break-point in the module code and the code is executing as expected. But a break-point in the Page_Load event handler shows that the dependencies are all null.
The property in question is public, with a setter and getter, and is marked with the Dependency attribute. I've tried with and without the attribute, with and without a mapping name, ... every combination I could think of and nothing works.
What am I missing???
It depends what you expect. Most exemples targeting MVC present custom controller factory which allows creating controllers with dependency injection. This is indeed also possible with web forms but instead of controller you must inject dependencies into pages. To do this you must replace PageHandlerFactory with custom implementation.
You can create your own implementation of PageHandlerFactory which will be able to resolve pages directly and inject dependencies defined in constructor or you can use one of these (here, here and here) which instead uses stantandard PageHandlerFactory and builds page instance (resolve property dependencies).

Web service is expecting a DataSet object, how can I provide that via ColdFusion or in raw XML?

I need to make a call to a web service written in .NET. The application making the call is written in ColdFusion. One of the parameters the web service expects is a DataSet object. I can't instantiate a .NET DataSet object in ColdFusion, how can I pass the web service something it will accept? I have no problem writing the SOAP request in raw XML, I just don't know what the XML for a DataSet object would look like.
All objects that .NET expects are serialized by Axis and are available to you. Unfortunately ColdFusion does not make it easy to get to.
To get to the stubs you must:
Access the WSDL in any way with coldfusion.
Look in the CF app directory for the stubs. They are in a "subs"
directory, organized by WSDL.like:
c:\ColdFusion8\stubs\WS\WS-21028249\com\foo\bar\
Copy everything from "com" on down into a new directory that exists in
the CF class path. or you can make one like:
c:\ColdFusion8\MyStubs\com\foo\bar\
If you created a new directory add it to the class path. and restart CF services.
Use them like any other java object with or CreateObject()
MyObj = CreateObject("java","com.foo.bar.MyObject");
Your dataset object should be in there somewhere in whatever java format Axis decided it should be. Most likely you're going to need to do almost all of this in cfscript
EDIT FOR QUESTIONS
THe SOAP object will define the object structure and Axis will create methods for manipulating it. Take a look at the Java object that axis creates. Remember that you can use CFDUMP to look at the methods and properties.
Now I HAVE seen .NET objects that Axis gets confused by, like the dreaded non-generic collection that turns into a "ArrayOfAnyType". It's important for .NET developers to use Generics in their services so that Axis can define the arrays properly....if they don't then it sucks and you may not be able to work with it in soap.
but have no fear obi-won...there is another way. You can always interact with .NET web services in a XML/RPC kind of style. It's not automatic, its a lot of hand parsing of XML, it sucks, but sometimes it's the only way to do it. You should be able to get some help from .NET by hitting up the .asmx file without the "?wsdl" on the end. If you do that .NET will generate a bunch of documentation and examples of what the calls and the XML look like. In that case, you can just create the XML and pass it over the wire as specified by using cfhttp. Good Luck!
P.S. I should note also that as far as I know there is no way to mix hand rolled XML with the ColdFusion/Apache Axis objects, there is also no way to model your own object for use with CF/Axis...you must use the stubs or nothing
Could you use JSON?
http://json.org/

Method 'XYZ' cannot be reflected

We have consumed a third party web service and are trying to invoke it from an ASP.NET web application.
However when I instantiate the web service the following System.InvalidOperationException exception is thrown:
Method 'ABC.XYZ' can not be reflected.
System.InvalidOperationException:
Method 'ABC.XYZ' can not be reflected.
---> System.InvalidOperationException: The XML element 'MyDoc' from namespace
'http://mysoftware.com/ns' references
a method and a type. Change the
method's message name using
WebMethodAttribute or change the
type's root element using the
XmlRootAttribute.
From what I can gather there appears to be some ambiguity between a method and a type in the web service.
Can anyone clarify the probably cause of this exception and is there anything I can do to rectify this or should I just go to the web service owners to rectify?
Edit: Visual Studio 2008 has created the proxy class. Unfortunately I can't provide a link to the wsdl as it is a web service for a locally installed thrid party app.
I ran into the same problem earlier today.
The reason was - the class generated by Visual Studio and passed as a parameter into one of the methods did not have a default parameterless constructor. Once I have added it, the error had gone.
It seems the problem is down to data type issues between VS and the web service that was written in Java.
Ultimately it was fixed by manually editing the class and schema files that were created by VS.
I have come across the exact same problem when I was consuming a 3rd party web service. The problem in this instance was that the mustUndertand property in the reference file was looking for a Boolean, whereby the namespace property looked for a string.
By looking through the reference i was able to idenitfy the offending property and simply add "overrides" to the method signature.
Not ideal as any time you update the service you have to do this but I couldn't find any other way around this.
To find the reference file select "all files" from the solution explorer
Hope this helps
I'm guessing the wsdl emitted by or supplied with the service is not in a form that wsdl.exe or serviceutil can understand - can you post the wsdl or link to it?
how are you creating the proxy classes?
Also you might like to try and validate the wsdl against the wsdl schema to check its valid
In my case I was getting a "method cannot be reflected" error due to that fact that in the class being returned by method, I had failed to expose a default parameter-less constructor.
I was working in VB.NET. In my return class I had declared a "New(..)" method that took a couple parameters (because that is how I wanted to use it in my code). But by doing so, I had supressed the default (hidden) parameterless New() constructor that VB adds behind the scenes. Apparently the web service handler requires that a parameterless constructor be available. As soon as I added back into my class a parameterless New() constructor, it all worked fine.
I got the same message but mine was caused by a missing System.Runtime.Serialization.dll since I tried to run a 3.5 application on a machine with only .NET 2.0 installed.
I had the same issue but I found that one of the WebMethod parameters has a member that is of type interface that is why VS could not serialise it. here is the exception when trying to download the disco file
System.InvalidOperationException: Cannot serialize member
'Leopard.JobDespatchParameters.SendingUser'
of type 'Leopard.Interfaces.IUser', see inner exception for more
details. ---> System.NotSupportedException: Cannot serialize member
Leopard.JobDespatchParameters.SendingUser
of type Leopard.Interfaces.IUser because it is an interface.
Old thread but I had a different issue, Maybe of help to someone. referenced dlls were mixed up between two versions on data layer and service layer that caused the problem.
Another scenario where this error can happen: I simply had another web method with the same name (but different parameters)in my web service that slipped in during a code merge. After I deleted the old method it worked.

Resources