Tag: .net

.NET API更新包括ID

我来自Ruby on Rails API背景,但我目前正在开发.NET C#WebAPI。 我熟悉C#和.NET webforms。 我正在尝试设置一个PUT请求来更新数据库中的记录。 scaffolded方法会覆盖所有字段,而我只想简单地更新通过PUT传递的字段。 我尝试使用以下代码: // PUT: api/Users/5 [ResponseType(typeof(void))] public IHttpActionResult PutUser(string id, User user) { if (!ModelState.IsValid) { return BadRequest(ModelState); } User userToUpdate = db.Users.Where(u => u.id == id).FirstOrDefault(); if (userToUpdate == null) { return NotFound(); } db.Entry(userToUpdate).CurrentValues.SetValues(user); try { db.SaveChanges(); } catch (DbUpdateException) { if (UserExists(id)) { return […]

.NET远程处理exception:权限被拒绝:无法远程调用非公共或静态方法

我正在编写一个程序,它允许加载特定的托管.DLL文件并使用它。 由于我希望能够卸载.DLL文件,我正在创建两个AppDomain – 一个用于应用程序本身,另一个用于当前加载的.DLL。 由于加载的.DLL中的大多数对象都没有很好地序列化,我正在创建一个MarshalByRefObject包装类,它将对象本身保存在自己的AppDomain中,并将一些reflection函数暴露给主应用程序AppDomain。 但是,当我尝试在远程对象上调用方法时,我遇到了exception: 权限被拒绝:无法远程调用非公共或静态方法。 这很奇怪,因为我根本没有使用任何非公共或静态方法。 从本质上讲,我所拥有的是: class RemoteObjectWrapper: MarshalByRefObject { private Type SourceType; private object Source; public RemoteObjectWrapper(object source) { if (source == null) throw new ArgumentNullException(“source”); this.Source = source; this.SourceType = source.GetType(); } public T WrapValue(object value) { if ( value == null ) return default(T); var TType = typeof(T); if […]

从SynchonizationContext派生

简而言之,我已经实现了一个派生自SynchronizationContext的类,以便GUI应用程序可以轻松使用在GUI线程以外的线程上引发的事件。 我非常感谢对我的实施的评论。 具体来说,你有什么建议反对或可能导致我没有预料到的问题吗? 我最初的测试成功了。 长版本:我目前正在开发分布式系统(WCF)的业务层,它使用回调将事件从服务器传播到客户端。 我的设计目标之一是提供可绑定的业务对象(即INotifyPropertyChanged / IEditableObject等),以便在客户端轻松使用这些对象。 作为其中的一部分,我提供了回调接口的实现,该接口在事件进入时处理事件,更新业务对象,进而引发属性更改事件。 因此,我需要在GUI线程上引发这些事件(以避免跨线程操作exception)。 因此,我尝试提供自定义SynchronizationContext,实现回调接口的类使用它来将事件传播到GUI线程。 此外,我希望此实现独立于客户端环境 – 例如WinForms GUI应用程序或ConsoleApp或其他东西。 换句话说,我不想假设静态SynchronizationContext.Current可用。 因此我使用ExecutionContext作为后备策略。 public class ImplicitSynchronisationContext : SynchronizationContext { private readonly ExecutionContext m_ExecContext; private readonly SynchronizationContext m_SyncContext; public ImplicitSynchronisationContext() { // Default to the current sync context if available. if (SynchronizationContext.Current != null) { m_SyncContext = SynchronizationContext.Current; } else { m_ExecContext […]

易失性IEnlistmentNotification和TransactionScope.AsyncFlowEnabled = true

除了.NET 4.5.1之外,TransactionScope上还有一个新选项,可以使用异步流。 这允许编写以下客户端代码 using(var txt = new TransactionScope(…, TransactionScopeAsyncFlowOption.Enabled) { await sender.SendAsync(); } 到现在为止还挺好。 但是当我需要实现一个易变的IEnlistmentNotification时,我正在努力做到这一点。 让我们假设以下场景,假设:我的底层基础架构从下到上完全异步 public class MessageSender : ISendMessages { public async Task SendAsync(TransportMessage message, SendOptions options) { await sender.SendAsync(message); } } 所以我想要实现的是引入像这样的易失性IEnlistmentNotification: internal class SendResourceManager : IEnlistmentNotification { private readonly Func onCommit; public SendResourceManager(Func onCommit) { this.onCommit = onCommit; } public void […]

通过MVC Web App中的ViewModel调用Graph API

我正在尝试使用Graph API来创建我自己的Web应用程序导航栏的“用户配置文件”部分。 为此,我对我的UserProfile控制器的GetUser操作进行了AJAX调用: $.ajax({ type: “GET”, url: “@Url.Action(“GetUser”, “UserProfile”, null)”, dataType: “json”, success: function (data, status, xhr) { console.log(“in AJAX”); $(“.img-circle, .user-image”).attr(“src”, data.Picture); $(“#user-menu-expanded”).text(data.User.DisplayName + ” – ” + data.User.JobTitle); $(“#user-menu-spinner”).remove(); console.log(data); }, error: function (ex) { console.log(ex); } }); 控制器将我的UserProfileViewModel作为Json返回,我用它来替换上面的元素,如我的AJAX成功函数所示。 UserProfile控制器: public JsonResult GetUser() { var model = new UserProfileViewModel(); return Json(model, JsonRequestBehavior.AllowGet); } […]

当我没有放置命名空间时,我的类有什么命名空间?

如果我没有在我的类中添加命名空间(vs.net 2008),哪个命名空间可以构建我的类? 更新它很奇怪,当我添加一个命名空间时,我似乎无法从我的用户控件引用类方法。 如果我明确设置一个命名空间,并在我的控件中添加一个’using …’,我仍然看不到它! 怎么会这样? using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; /// /// Summary description for Globals /// public class Globals { public Globals() { } public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings[“MyDb”].ConnectionString; } } } 我的控制: using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Linq; using […]

抽象工厂模式和属性

我有点新设计模式,这是我在stackoverflow中的第一篇文章,所以希望这个问题有意义。 我已经创建了一个抽象工厂来处理为不同的图表供应商(dundas,flash等等)生成xml字符串。 下面是我工厂的代码大纲(如果它有帮助,我可以包含更多。)我希望我的客户能够设置所有类型的图表(标题,动画等)中常见的属性所以客户可以做这样的事情: GraphCreator fusion = new FusionGraphs(); //set the props for the graph fusion.Caption = “Fusion 2D Line Chart”; 做这个的最好方式是什么? 现在,我在抽象创建器中设置属性,以便客户端可以访问它们,但我还必须在我的工厂中复制这些属性,以便我可以在构建xml时访问它们。 //这是抽象工厂 public interface IXMLFactory { //add interface methods IRoot makeRoot(); IRootAttrib makeRootAttrib(); INodes makeNodes(); INodeAttrib makeNodeAttrib(); } //这是抽象的创作者 public abstract class GraphCreator { public virtual Graph getGraph(Graph.Types graphType) { //abstract product Graph graph; […]

在.net中更改MDI容器表单的背景颜色或背景图像

我需要在我的应用程序中更改mdi父级的背景颜色或背景图像。 我尝试更改背景颜色或指定背景图像,它将无法正常工作。 我也尝试循环窗体中的控件以获取mdiclient并更改其背景颜色,也是同样的结果。

服务结构中的配置转换?

当您通过Visual Studio创建一个空的Service Fabric应用程序时,项目模板将自动为您创建不同的配置文件,如Node1.XML,Node5.XML等。 我们更少关注节点数量而不是我们所针对的环境。 只有1个配置文件并从中创建转换,与Service Fabric建议相反吗? 例如,我们可能会有这样的事情: <!– ClusterConnectionParameters allows you to specify the PowerShell parameters to use when connecting to the Service Fabric cluster. Valid parameters are any that are accepted by the Connect-ServiceFabricCluster cmdlet. For a remote cluster, you would need to specify the appropriate parameters for that specific cluster. For example: Example […]

DotNetOpenAuth OAuth2访问额外数据

我正在使用DotNetOpenAuth的OAuth2库来处理与另一个第三方系统的授权。 这一切都很有效,除了第三方系统在使用AccessToken的Response中返回UserId =“testname”。 我需要UserId,因为这个第三方API需要它作为API调用的一部分(例如:users / {userId} / account)。 使用DotNetOpenAuth,我无法访问AccessToken响应,因此无法获取UserId。 我正在调用:( _client是一个WebServerClient)var state = _client.ProcessUserAuthorization(request); state有我的AccessToken,但没有发送的额外数据。 基于DotNetOpenAuth源代码,UserId进入库内,我没有任何访问权限。 无论如何使用DotNetOpenAuth获取UserId? 或者我是否需要放弃DotNetOpenAuth并尝试其他方法?