简体   繁体   中英

How do I reference a file in a WCF Webservice application?

I've worked on a few C# projects over the years, and generally the rules are consistent from project type to project type. But having started a WCF Webservice project, I'm finding things a bit different. I have to validate incoming XML to a schema. I've created a folder off the project root, XSDs, for storing the schemas. In previous projects, when referencing an XSD in a folder, I've used something like this:

            XmlTextReader textReader = null;
            XmlSchemaCollection xSchemaCollection = null;
            XmlValidatingReader valReader = null;

            string uri = string.Format(@"{0}\{1}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), XSDPath);

            //  text reader object
            textReader = new XmlTextReader(uri);
            xSchemaCollection = new XmlSchemaCollection();
            xSchemaCollection.Add(null, textReader);

            //  XML validator object
            valReader = new XmlValidatingReader(strXMLDoc, XmlNodeType.Document, null);

The properties for the XSD files are set to Copy Always to target folder. Normally, I get a valid path to the XSD, and things proceed just fine. But in this WCF Webservice project, the path that the XSD files get copied to is a temporary directory, and I don't get a valid path. The xSchemaCollection.Add method fails.

BTW, the XSD folder isn't in the WCF project at this point. I added a class library project (in which the above code currently resides), and that's where the folder and files are. I expected this to eliminate the temp-directory problem, but it didn't.

Any ideas what I'm doing wrong? Thanks in advance.

WCF is a very powerful framework, but one of the core concepts of WCF is that the service definition is abstracted away from the implementation details of the communication channel. This means that a WCF service has no knowledge of what XSD files are used to describe its content, or even if XSD files are used at all (a service binding doesn't necessarily use SOAP).

WCF works really well if you're trying to follow an existing protocol, or if you're trying to customize one aspect of a complex protocol in which every other aspect of the protocol conforms to web standards. However, it sounds like what you're doing is more down and dirty than that.

If you find that you must process SOAP messages directly in a way that is non-standard, WCF may not be the best tool for the job. I would recommend that you consider using other service implementation strategies, such as an HttpHandler or a good old asmx file. Asmx based web services are nowhere near as sophisticated as WCF, but it is this very lack of sophistication that makes them more flexible for non-standard protocols.

I think that if, for some reason, you have an issue to access the XSDs via the file system, then I would most likely embed them as resources within an assembly. All you have to do then is to use a custom resolver (from the sound of it, these XSDs don't seem to make use of includes/imports; it they do, the custom resolver is definitely the way to go)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM