从URL访问的简单WCF服务 [英] Simple WCF Service accessing from URL
本文介绍了从URL访问的简单WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法从我的项目的URL访问我的wcf服务。
这是我正在做的基本示例。
命名空间 RoyalBatteriesWS
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = GET,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = SayHello)]
string SayHello();
}
}
.svc file
命名空间 RoyalBatteriesWS
{
public class 默认值:IService
{
public string SayHello()
{
return Say Hello;
}
}
我的配置:
< ?xml version = < span class =code-string> 1.0?>
< configuration>
< connectionStrings>
< add name = ServiceConnection connectionString = 数据源=。\SQLEXPRESS; AttachDbFilename = | DataDirectory | \Database1.mdf; Integrated Security = True; User Instance = True providerName = System.Data.SqlClient />
< / connectionStrings >
< system.web>
< compilation debug = true targetFramework = 4.0 />
< / system.web >
< system.serviceModel>
< services>
< service behaviorConfiguration = SimpleServiceBehavior name = RoyalBatteriesWS.Default >
< endpoint address =
binding = basicHttpBinding
contract = RoyalBatteriesWS。 IService />
< endpoint address = mex
binding = mexHttpBinding
contract = IMetadataExchange />
< / 服务 >
< / services >
< behavior>
< serviceBehaviors>
< behavior name = SimpleServiceBehavior >
< serviceMetadata httpGetEnabled = True policyVersion = Policy15 />
< / 行为 >
< / serviceBehaviors >
< endpointBehaviors>
< / endpointBehaviors >
< / 行为 >
< serviceHostingEnvironment multipleSiteBindingsEnabled = true />
<! - < serviceHostingEnvironment aspNetCompatibilityEnabled = true /> - >
< standardEndpoints>
< webScriptEndpoint>
< standardEndpoint name = crossDomainScriptAccessEnabled = true />
< / webScriptEndpoint >
< / standardEndpoints >
< / system.serviceModel >
< system.webServer>
< modules runAllManagedModulesForAllRequests = true />
< httpProtocol>
< customHeaders>
< add name = Access-Control-Allow-Origin value = * />
< add name = Access-Control-Allow-Headers value = * />
< / customHeaders >
< / httpProtocol >
< / system.webServer >
< / configuration >
当我运行网址时:
http:// localhost:2305 / Service1.svc / SayHello [ ^ ]
我得到400 chrome中的错误请求错误。
如果我在这里遗漏了某些内容,请告诉我。
解决方案
I am unable to access my wcf service from URL for my project.
Here's a basic example of what I am doing.
namespace RoyalBatteriesWS
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "SayHello")]
string SayHello();
}
}
.svc file
namespace RoyalBatteriesWS
{
public class Default : IService
{
public string SayHello()
{
return "Say Hello";
}
}
My Config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ServiceConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="SimpleServiceBehavior" name="RoyalBatteriesWS.Default">
<endpoint address=""
binding="basicHttpBinding"
contract="RoyalBatteriesWS.IService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SimpleServiceBehavior">
<serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />-->
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
When i run the url :
http://localhost:2305/Service1.svc/SayHello[^]
I get a 400 Bad Request error in chrome.
Please let me know if i am missing something here.
解决方案
这篇关于从URL访问的简单WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文