(一)这一种是基地+address
写两个<endpoint>
第一个是业务用的,第二个就是元数据交换,地址是: net.pipe://localhost/Dmc3000IAxisAPI/mex,如果你用浏览器可以访问到,地址就是对的。
当然也可以不要基地址,直接写元数据的地址。
<service name="SMotionHardwareLayer.MotionCardRes.DMC3400A.AxisRealization" behaviorConfiguration="Dmc3000IAxisAPI"> <host> <baseAddresses> <add baseAddress="net.pipe://localhost/Dmc3000IAxisAPI"/> </baseAddresses> </host> <endpoint address="" binding="netNamedPipeBinding" bindingConfiguration="" contract="SMotionHardwareLayer.HardwareLayerService.IAxis"/> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service>
(二)弄一个behaviorConfiguration,它设置httpGetEnable="true", httpGetUrl=元数据的地址
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService"> <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="Contracts.ICalculator" /> </service> </services> </system.serviceModel>
错误示例:
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceDebug /> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Services.CalculatorService" behaviorConfiguration="NewBehavior0"> <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" bindingConfiguration="" contract="Contracts.ICalculator" /> <endpoint address="http://127.0.0.1:9999/meta" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> </system.serviceModel>
会报错,如下:
System.InvalidOperationException:“ServiceMetadataBehavior 的 HttpGetEnabled 属性设置为 True,
而 HttpGetUrl 属性是相对地址,但没有 http 基址。请提供 http 基址或将 HttpGetUrl 设置为绝对地址。”
它说的是元数据的address它是个相对地址,你要么必须指定基地址;
要么用代码的方式设置HttpGetURL为绝对地址,注意,这里你还不能用配置方式指定绝对地址!
上面配置中,元数据的address="http://127.0.0.1:9999/meta" 是企图指定绝对地址,但这样是不行的。
正确的做法:
第一步在配置里面修改:
两个地方改变了:
服务的address变空串了。
元数据的address="mex"
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceDebug /> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="NewBehavior0" name="Services.CalculatorService"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="" contract="Contracts.ICalculator" /> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> </system.serviceModel>
第二步在hosting的初始化代码里传入基地址:
static void Main(string[] args) { Uri baseUri = new Uri("http://127.0.0.1:9999/calculatorservice"); using (ServiceHost host = new ServiceHost(typeof(CalculatorService), baseUri)) { host.Opened += delegate { Console.WriteLine("CalculaorService已经启动,按任意键终止服务!"); }; host.Open(); Console.Read(); } }
最终的地址就是基地址+相对地址。
这样,服务的address就是:
http://127.0.0.1:9999/calculatorservice
元数据的address就是:
http://127.0.0.1:9999/calculatorservice/mex
另外特别注意的是,http://127.0.0.1:9999/calculatorservice/mex在浏览器中可能访问不了。
不要把浏览器访问是否成功做为元数据是否发布成功的方式!!!
你应该在VS里面做服务引用,这个才是正确的做法!
如果浏览器访问不了,可以用基地址+?wsdl的方式访问,即:
http://127.0.0.1:9999/calculatorservice?wsdl

