Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

.net - ServiceRoute + WebServiceHostFactory kills WSDL generation? How to create extensionless WCF service with ?wsdl

I'm trying to use extension-less / .svc-less WCF services. Can anyone else confirm or deny the issue I'm experiencing?

I use routing in code, and do this in Application_Start of global.asax.cs:

RouteTable.Routes.Add(new ServiceRoute("Data", new WebServiceHostFactory(), typeof(DataDips)));

I have tested in both IIS 6 and IIS 7.5 and I can use the service just fine (ie my extension-less wildcard mapping handler config is correctly pointed at ASP.NET). However, metadata generation is totally screwed up. I can hit my /mex endpoint with the WCF Test Client (and I presume svcutil.exe) -- but the ?wsdl generation you typically get with .svc is toast. I can't hit it with a browser (get 400 bad request), I can't hit it with wsdl.exe, etc. Metadata generation is configured correctly in web.config.

This is a problem of course, because the service is exposed as basicHttpBinding so that an old style ASMX client can get to it. But of course, the client can't generate the proxy without a WSDL description.

If I instead use serviceActivation routing in config like this, rather than registering a route in code:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
  <serviceActivations>
    <add relativeAddress="Data.svc" service="DataDips" />
  </serviceActivations>
</serviceHostingEnvironment>

Then voila... it works.

But then I don't have a clean extensionless url. If I change relativeAddress from Data.svc to Data, then I get a configuration exception as this is not supported by config. (Must use an extension registered to WCF).

I've also attempted to use this code in conjunction with the above config:

RouteTable.Routes.MapPageRoute("","Data/{*data}","~/Data.svc/{*data}",false);

My thinking is that I can just point the extensionless url at the configured .svc url. This doesn't work -- the /Data.svc continues to work, but /Data returns a 404.

I did find a work-around if using urlMappings in config like this, in conjunction with serviceActivation above:

<urlMappings>
   <add url="~/Data" mappedUrl="Data.svc"/>
</urlMappings>

The problem with this is twofold - 1. It seems convoluted 2. In the generated WSDL, the operation endpoints still refer to Data.svc/, rather than Data/ -- therefore theres dependency on Data.svc actually existing / responding.

This is not really what I want, even if it kinda / sorta solves the problem.

Is there a proper way of getting extension-less WCF service urls to generate WSDL correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wow, do I feel dumb.

I should have been hosting my service with ServiceHostFactory, NOT WebServiceHostFactory.

As soon as I moved back to code from config... and swapped to this line of code:

RouteTable.Routes.Add(new ServiceRoute("Data", new ServiceHostFactory(), typeof(DataDips)));

I was in business with an extensionless URL serving up WSDL and help pages.

It's too bad I wasted so much time on this. It was an accident that I was using WebServiceHostFactory, but there's no disclaimer on the factory page in MSDN about the reduced functionality. (I would argue that removing WSDL makes sense, but removing the Help pages does not as they could simply provide an attribute in config to rename the 'Help' page should there be a REST operation with that name... sigh).

There is a note in the WebServiceHost docs http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...