使用WCF测试客户端调用以下方法
Service1.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace ShoppingCartService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
DigitalXDBEntities _db = new DigitalXDBEntities();
//Fetches Images from SQL Database
public string GetImage(object Picture)
{
return "data:image/jpg;base64," + Convert.ToBase64String((byte[])Picture);
}
//Fetches popular products
public List<Product> GetTopProducts()
{
var query = (from p in _db.Products
orderby p.Price
select p).Take(5);
return query.ToList();
}
//Fetches all DVD using subcategory product id
public List<Product> GetDvds()
{
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
var query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p);
return query.ToList();
}
//Fetches all components using subcategory product id
public List<Product> GetComponents()
{
IEnumerable<int> list = new List<int>() { 25, 31, 29, 30 };
List<Product> query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p).ToList();
return query;
}
//Fetches all Consoles using product id
public List<Product> GetConsoles()
{
IEnumerable<int> list = new List<int>() { 21, 23 };
List<Product> query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p).ToList();
return query;
}
//Fetches all games using subcategory product id
public List<Product> GetGames()
{
IEnumerable<int> list = new List<int>() { 9, 10, 11, 12, 14, 15 };
List<Product> query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p).ToList();
return query;
}
//Fetches all handbooks using subcategory product id
public List<Product> GetHandbooks()
{
IEnumerable<int> list = new List<int>() { 27, 28 };
List<Product> query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p).ToList();
return query;
}
//Fetches all pc parts using subcategory product id
public List<Product> GetPcParts()
{
IEnumerable<int> list = new List<int>() { 26 };
List<Product> query = (from p in _db.Products
where list.Contains(p.SubCategoryID)
select p).ToList();
return query;
}
}
}
我的Iservice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace ShoppingCartService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Product> GetDvds();
[OperationContract]
List<Product> GetTopProducts();
[OperationContract]
List<Product> GetComponents();
[OperationContract]
List<Product> GetGames();
[OperationContract]
List<Product> GetHandbooks();
[OperationContract]
List<Product> GetPcParts();
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
}
我的webconfig
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<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" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
<connectionStrings>
<add name="DigitalXDBEntities" connectionString="metadata=res://*/DigitalX.csdl|res://*/DigitalX.ssdl|res://*/DigitalX.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-D4D7MNA;initial catalog=DigitalXDB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
完整错误消息
无法调用该服务。可能的原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理,还原到默认配置或刷新服务来恢复。
接收到http://localhost:56504/Service1.svc的HTTP响应时发生错误。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。
服务器堆栈跟踪:at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(引发WebException webException,HttpWebRequest请求,HttpAbortReason abortReason)
在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)at System.ServiceModel.Channels.RequestChannel.Request(消息消息, TimeSpan超时)at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息 消息,TimeSpan超时)at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway,ProxyOperationRuntime操作,Object [] ins, 对象[]出局,TimeSpan超时)at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime operation)at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即时聊天 消息)在[0]处重新抛出异常:at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天 reqMsg,IMessage retMsg)at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培; msgData,Int32类型)在IService1.GetDvds()处 Service1Client.GetDvds()
内部异常:底层连接已关闭:意外 接收时发生错误。在 System.Net.HttpWebRequest.GetResponse()at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度 超时)
内部异常:无法从传输连接中读取数据: 远程主机强行关闭现有连接。在 System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量, 在System.Net.PooledStream.Read(Byte []缓冲区,Int32中的Int32大小) offset,Int32 size)at System.Net.Connection.SyncRead(HttpWebRequest请求,布尔值 userRetrievedStream,Boolean probeRead)
内部异常:现有连接被强行关闭 System.Net.Sockets.Socket.Receive(Byte []缓冲区的远程主机, Int32偏移量,Int32大小,SocketFlags socketFlags)at System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量, Int32尺寸)
我遇到过上一篇文章,提到将IEnumerable列表更改为通用列表,遗憾的是无法正常工作
编辑:帖子已更新完整代码和命名空间
编辑:使用服务提供的默认方法(如下所示)在测试客户端中工作得很好,但连接到数据库的我自己的方法根本不会调用并显示上面的错误消息
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
答案 0 :(得分:0)
在配置文件中检查部件是否有WCF配置,您将看到您没有endpoint
和service contract
的任何配置。基本上你完全错过了services
下的<system.serviceModel>
部分,我相信这是问题
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
根据您编辑的帖子,它应该类似于以下内容
<services>
<service name="ShoppingCartService.Service1">
<endpoint address="" binding="basicHttpBinding" contract="ShoppingCartService.IService1" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>