【发布时间】:2015-09-17 21:51:13
【问题描述】:
由于我使用的是 Windows phone 8.1,因此我无法像以前的应用程序那样调用我的 WCF 服务。
经过一番搜索,我找到了以下从 windows phone 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>
【问题讨论】:
-
您能发布您的服务的外观吗?您必须生成 WCF 服务的代理并将其包含在推送到 windows phone 的代码中,以使用使用 proxyclient 类而不是使用 httpClient 的方法
-
@Rajesh 我已按要求添加了 WCF 代码,但不知道需要什么以及应该省略什么。为了使这个问题对未来的读者更具可读性,请告诉我可以删除哪些内容
-
查看您的服务,您必须使用 svcutil 和 wsdl 创建服务的客户端代理。生成客户端代理后,将其包含在您的应用程序中以执行对 WCF 服务的方法调用。这应该与任何希望与 WCF 服务通信的 .NET 客户端非常相似
标签: c# wcf windows-phone-8.1