当我使用Windows phone 8.1时,我无法像以前的应用程序那样调用我的WCF服务。
经过一番搜索后,我能够找到以下替代方案,从窗口电话8.1
呼叫服务try
{
string responseBodyAsText;
HttpResponseMessage res = await httpClient.GetAsync("http://localhost:50436/FabSvc.svc");
res.EnsureSuccessStatusCode();
string status = res.StatusCode + " " + res.ReasonPhrase + Environment.NewLine;
responseBodyAsText = await res.Content.ReadAsStringAsync();
responseBodyAsText = responseBodyAsText.Replace("<br>", Environment.NewLine); // Insert new lines
string output = responseBodyAsText;
}
catch (HttpRequestException hre)
{
string status = hre.ToString();
}
catch (Exception ex)
{
// For debugging
string status = ex.ToString();
}
此代码返回状态代码OK(见下文)以及一些基本数据。但我的问题是,我无法解决从这一点开始访问任何方法的问题。我做错了什么?
编辑:当前的WCF服务(简化)
service.svc
namespace ServiceName.WCF
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FabSvc : ICollection, ICompositionType
{
/// <summary>
/// Pings this instance.
/// </summary>
/// <returns></returns>
public bool Ping()
{
return true;
}
/// <summary>
/// Gets all CompositionTypes.
/// </summary>
/// <returns></returns>
public List<CompositionTypeBase> GetAllCompositionTypes()
{
List<CompositionTypeBase> allCompositionTypes = new List<CompositionTypeBase>();
try
{
allCompositionTypes = CompositionTypeBase.GetAllCompositionTypes();
}
catch (Exception ex)
{
throw ex;
}
return allCompositionTypes;
}
}
Icollection.cs
namespace ServiceName.WCF
{
[ServiceContract]
interface ICollection
{
[OperationContract]
List<CollectionBase> GetAllCollections();
}
}
CollectionBase.cs
namespace ServiceName.WCF.Entities
{
public class CollectionBase
{
public int CollectionID { get; set; }
public string Description { get; set; }
/// <summary>
/// Gets all seasons.
/// </summary>
/// <returns></returns>
public static List<CollectionBase> GetAllCollections()
{
List<CollectionBase> allCollections = new List<CollectionBase>();
IEnumerable<Collection> collections = Collection.GetAll();
foreach (var collection in collections)
{
CollectionBase seasonBase = new CollectionBase();
seasonBase.CollectionID = collection.CollectionID;
seasonBase.Description = collection.Description;
allCollections.Add(seasonBase);
}
return allCollections;
}
}
}
Web.config
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="ImageFilePath" value="C:\websites\images\"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="20000000" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="50" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="20000000" maxReceivedMessageSize="20000000" maxBufferSize="20000000" openTimeout="12:00:00" receiveTimeout="12:00:00" sendTimeout="12:00:00" closeTimeout="12:00:00">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
<connectionStrings>
<add name="ServiceNameEntities" connectionString="CONNECTION_STRING_INFO" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
答案 0 :(得分:0)
当您使用HttpClient时,您打算将数据发布到端点,因此这里没有合约。
你&#34;看&#34;创建代理时的操作,通过addind引用或使用wcfutil。
对于RestFul服务,您只需调用此操作:
HttpResponseMessage res = await httpClient.GetAsync("http://localhost:50436/FabSvc.svc/GetAllCollections/value");
但是您的服务期望SOAP请求,因此您需要使用SOAP / XML发出请求以使用您的方法。 在SOAP信封内,您可以通知服务您想要的操作,如下所示:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<OperationName xmlns="http://tempuri.org/">
您还可以在标题中包含该操作。 在下面的例子中,我使用了WebClient:
using (var client = new WebClient())
{
var data = GetSoap();
client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
client.Headers.Add("SOAPAction", "\"http://tempuri.org/ICollection/GetAllCollections\"");
var response = client.UploadString("http://localhost:50436/FabSvc.svc", data);
Console.WriteLine(response);
}
希望有所帮助