我已经阅读了几篇关于构建自定义ErrorHandler的文档(通过继承IErrorHandler).不幸的是,我对如何做到有几点疑虑.
问题是我并不完全理解IErorrHandler的两个方法的含义(即ProvideFault和HandleError).对我来说,HandleError用于处理异步逻辑(例如登录).但是,在这种情况下,为什么这个方法返回一个布尔值?我还认为可以使用另一种方法来确定错误是应该传播到客户端还是传播给其他人.
我错了吗 ?
我有一个同步Web服务调用,它返回一条消息.我需要快速返回一条消息,该消息基本上表明订单已收到.然后我需要花几分钟处理订单,但不能阻止服务电话那么长时间.那么我如何从Web服务返回,然后再做一些东西呢?我猜我需要在返回之前分叉一些其他线程或东西,但我不确定最好的方法.
string ProcessOrder(Order order)
{
if(order.IsValid)
{
return "Great!";
//Then I need to process the order
}
}
Run Code Online (Sandbox Code Playgroud) 在处理个人项目时,我想要一个简单的服务,从Outlook中提取项目,并在"RESTful"设计中在WCF中托管.在这个过程中,我提出了这个相当野兽的课程.
人们见过的其他可怕的linq代码是什么?
public IQueryable<_AppointmentItem> GetAppointments(DateTime date)
{
var dayFlag = (OlDaysOfWeek)(int)Math.Pow(2, (int)date.DayOfWeek);
return
OlDefaultFolders.olFolderCalendar.GetItems<_AppointmentItem>()
.Select(a => new
{
Appointment = a,
RecurrencePattern = a.IsRecurring ?
a.GetRecurrencePattern() : null
})
.Where(a =>
a.Appointment.Start.Date <= date &&
(
(a.RecurrencePattern == null &&
a.Appointment.End.Date >= date) ||
(
a.RecurrencePattern != null &&
(
(a.RecurrencePattern.DayOfMonth == 0 ||
a.RecurrencePattern.DayOfMonth == date.Day) &&
(a.RecurrencePattern.DayOfWeekMask == 0 ||
((a.RecurrencePattern.DayOfWeekMask &
dayFlag) != 0)) &&
(a.RecurrencePattern.MonthOfYear == 0 ||
a.RecurrencePattern.MonthOfYear == date.Month)
)
)
)
)
.Select(a …Run Code Online (Sandbox Code Playgroud) 我在StackOverflow上的另一篇文章中关注了这个链接:
这是我的WCF服务库中的app.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service behaviorConfiguration="TestClient.Service.Service1Behavior"
name="TestClient.Service.SearchService">
<endpoint address="" behaviorConfiguration="Sample.WsdlSampleEndpointBehavior" binding="basicHttpBinding" bindingConfiguration=""
contract="TestClient.Service.ISearchService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/TestClient.Service/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Sample.WsdlSampleEndpointBehavior">
<wsdlExtensions location="http://localhost:8731/TestClient.Service/Service1/"/> …Run Code Online (Sandbox Code Playgroud) 我们正在使用WCF的"Rest toolkit"中的HttpClient类(http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644)来连接我们创建的Rest服务器.
服务器当前总是关闭连接,无论"连接"标头(它正在开发中,所以现在可以).
如何告诉HttpClient实例始终关闭连接(或接受服务器关闭它)?我尝试添加"Connection:close"标头,但导致了一个exeption("连接"不是允许的标头).我也尝试设置DefaultHeaders.Connection.Close = true,但这似乎没有任何区别.POST完成后我仍然可以看到与netstat的连接.
(身体和uri是字符串)
HttpClient client = new HttpClient();
client.DefaultHeaders.Connection = new Connection();
client.DefaultHeaders.Connection.Close = true;
HttpContent content = HttpContent.Create(body);
HttpResponseMessage res = client.Post(new Uri(uri), content);
Run Code Online (Sandbox Code Playgroud)
这里的问题是,下次我们进行POST时,调用只是阻塞.我们认为这是由于客户端保持连接这一事实,服务器不支持这一点.
我正在迈出第一步,开始使用网络服务.这是我的情况:
我正在使用.NET 3.5编写现有的企业内部网站.我需要添加对Web服务的调用,传递帐户数据并获得响应.
我能够添加.wsdl文件的Web引用,并让Visual Studio生成服务引用类.
所以,这是我的问题 - 在这种情况下使用WCF更容易,还是应该使用ASP.NET Web服务架构中的烘焙?
我尝试使用以下代码使Web服务正常工作,但未成功.在此代码中,ProgramServiceClient是VS使用wsdl文件为我生成的服务类的名称.同样,我对此非常新,所以任何指针都会非常有用!
var client = new ProgramServiceClient();
Int64 acct = 123456781234
var requestMetadata = new RequestMetadata();
var response = new GetProgramResponse();
var request = new GetProgramRequest
{
AccountId = acct
};
client.GetProgram(requestMetadata, request, out response);
Run Code Online (Sandbox Code Playgroud) 我创建了一个名为AddServiceLibrary的类库,其中我有一个名为AssemblyLoader的方法,代码如下:
string executingAssemblyName = Application.ExecutablePath;
AssemblyName asmName = AssemblyName.GetAssemblyName(executingAssemblyName);
AppDomain appDomain = AppDomain.CurrentDomain;
Assembly assembly = appDomain.Load(asmName);
_assemblyTypes = assembly.GetTypes().ToList();
LoadAppConfig();
Run Code Online (Sandbox Code Playgroud)
此方法在当前appdomain中加载执行程序集.我有另一种叫做LoadAppConfig()的方法
ServicesSection serviceSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
ServiceElementCollection sereleColl = serviceSection.Services;
string endPointAddress = string.Empty ;
foreach (var ele in sereleColl)
{
_serviceType = GetServiceType((System.ServiceModel.Configuration.ServiceElement)(ele)).Name);
break;
}
ServiceHoster.HostService(_serviceType);
Run Code Online (Sandbox Code Playgroud)
此方法读取app.config文件并查找wcf服务的类型.我有一个类ServiceHoster我有一个方法HostService:
public static void HostService(Type serviceType)
{
using (ServiceHost host = new ServiceHost(serviceType))
{
host.Open();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个名为MyWCFService的不同项目,我在这个项目中添加AddServiceLibrary的引用并调用该方法;
AddServiceLibrary.LoadLibrary lb = new AddServiceLibrary.LoadLibrary();
lb.AssemblyLoader();
Run Code Online (Sandbox Code Playgroud)
我希望在这一点上我的服务是正确托管的,但是当我想在我的客户端项目中使用AddServiceReference时,它告诉我,由于目标机器主动拒绝它,因此 无法建立连接. 如果我不使用我的AddServiceLibrary,那么它是找到服务并且工作正常.请任何人看看它,并建议我的方法可能是错的.
我正在计划一个更大的项目,所以我正在考虑一些技术选择.
该项目将使用3层架构设计.表示层将是ASP.NET,但它可能是其他一些技术.现在这没关系.
我的问题是:
我会感激任何提示
两种方法MyBehavior :: CreateSerializer()都没有被调用,但是ReplaceBehavior()方法正在工作.它正在使用我的自定义更改默认行为.
有人知道问题出在哪里?
关键是编写自定义REST WCF序列化程序,该序列化程序应生成非XML文本格式结果.
public class MySerializerFormatAttribute : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters)
{
}
public void Validate(OperationDescription description)
{
}
private static void ReplaceBehavior(OperationDescription description)
{
DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcsOperationBehavior != null)
{
int idx = description.Behaviors.IndexOf(dcsOperationBehavior);
description.Behaviors.Remove(dcsOperationBehavior);
description.Behaviors.Insert(idx, new MyBehavior(description));
}
}
public void ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
{
ReplaceBehavior(description);
}
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
ReplaceBehavior(description);
}
};
public class MySerializer : XmlObjectSerializer
{
public override bool …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个简单的WCF应用程序,我在我的服务中添加了一个显示方法,但我不知道如何测试这个显示服务?我使用谷歌进行了搜索,但没有找到任何可行的解决方案.