Friday, May 20, 2011 by Nate Bross
Someone asked on Stack Overflow:
I am using the FileSystemWatcher Class. I am trying to pipe the output to a text file. I have added the StreamWriter fileWriter = new StreamWriter("test.txt"); but nothing is output to the file! Where am I going wrong?
class Program
{
static void Main(string[] args)
{
string dirPath = "C:\\";
FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
fileWatcher.IncludeSubdirectories = true;
fileWatcher.Filter = "*.exe";
// fileWatcher.Filter = "C:\\$Recycle.Bin";
// fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
// fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
// fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
fileWatcher.EnableRaisingEvents = true;
StreamWriter fileWriter = new StreamWriter("test.txt");
Console.ReadKey();
}
}
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
You need to call
fileWriter.Write(data);
Additionally, you should wrap it up like this:
using(StreamWriter fileWriter = new StreamWriter("test.txt"))
{
fileWriter.Write(data);
fileWriter.Flush(); // maybe not necessary
}
This will write data to the filesystem and it should trigger your FileSystemWatcher object.
edit — inplace example
class Program
{
static void Main(string[] args)
{
string dirPath = "C:\\";
FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
fileWatcher.IncludeSubdirectories = true;
fileWatcher.Filter = "*.exe";
// fileWatcher.Filter = "C:\\$Recycle.Bin";
// fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
// fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
// fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
fileWatcher.EnableRaisingEvents = true;
// updated code
using(StreamWriter fileWriter = new StreamWriter("test.txt"))
{
var data = true;
fileWriter.Write(data);
}
Console.ReadKey();
}
}
Notable comments
Nate (0 upvotes): Where are you defining e? I’m assuming the code sample you provided was dumbed down. If that is truely your entire code, there is no e object defined at the same scope that fileWriter is defined and that error is to be expected.
Nate (0 upvotes): Try something like fileWriter.Write(e.OldName + " was renamed to " + e.Name + Environment.NewLine);
Nate (0 upvotes): @Michael — yes, you can change that to whatever you want, its just an example. The StreamWriter.Write(); method has many overloads that take different parameters.
Nate (0 upvotes): @Michael glad to hear it. Good luck.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, May 18, 2011 by Nate Bross
Someone asked on Game Development:
I am making a android game, it’s a tower/base defense like type of game (yea I know it is kind of confusing), I was wondering if anyone knows a good way to have the attacking sprites randomly generate without the number of sprites being higher than the level. I want the attack sprites to equal the level. I have 2 sprite types that I want to be generated, I just don’t know how to make it to where one of the two randomly comes. Ex: you on level 4 and Sprite A spawns and then 3 others right after it but which version you get is random.
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
I presume you’re running in Android’s flavor of Java, so
For generating random numbers, see this.
You’ll need to setup a loop, which will load your extra sprites:
// you may wish to use a seed, so you can re-generate these numbers later, this depends on your use
Random gen = new Random();
for(int i = 0; i < currentLevelNumber; i++)
{
double r = generator.nextGaussian();
if(r > 0)
// sprite type A
else
// sprite type B
}
I’m assuming you already know how to load, draw, and update sprites. If not, start here.
Originally posted on Game Development — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, May 17, 2011 by Nate Bross
Someone asked on Stack Overflow:
Is TransportWithMessageCredential and wcf 3.5 and hosted in IIS supported?
I am using it with .net 4.0 and everythign seems to work fine.
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
Yes. I have used it, are you having issues back-porting to 3.5?
Notable comments
Nate (0 upvotes): All I can say, is that I have not had issues with TransportWithMessageCredential and .NET 3.5 SP1 on Windows Server 2008 R2.
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Monday, May 16, 2011 by Nate Bross
Someone asked on Stack Overflow:
What bindings and security configurations are best for an environment with Windows and Mac Computers?
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
BasicHttpBinding will probably have the greatest reach. Just about every platform supports it, since the data is simply sent as an HTTP POST to a specific Uri — any platform which has a web browser can use this binding.
You can do an HTTPs binding with BasicHttpBinding as well — so you can still secure your data. You can also use security like TransportWithMessageCredential to provide username/password validation.
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, May 3, 2011 by Nate Bross
Someone asked on Stack Overflow:
I created three projects in a solution, one web applciation, two class libraries for DAL and BLL. Created the Entity Framework model in the DAL layer and referenced the DAL library in the BLL project.
When I call the BLL objects from the web application project am running into problems, it says I need to reference entity framework. I dont want any dependency on DAL library objects in the web application project.
Is there any specific guidance on building clean three-tier applicaiton using Entity Framework.
I posted the following answer, which was chosen as the accepted answer and received 19 upvotes:
Sounds like your BLL is exposing the entity classes you added in the DAL. You’ll need to create wrapper classes (That are POCO) in the BLL and return those instead of the entities from the DAL.
This is probably what you are doing:
// DAL
// .edmx file generated entities
public IQueryable<TableEntity> GetTableEntities()
{
// read from entity framework and return
}
// BLL
public IEnumerable<TableEntity> ReadTableEntitiesForUser(int userID);
{
var d = new DAL();
var entities = d.GetTableEntities();
// restrict to entites this user "owns"
entities = entities.Where(e => e.OwnerID.Equals(userID));
return entities;
}
// WebApp
var b = new BLL();
var myEntities = b.ReadTableEntitiesForUser(1234);
This is probably what you should be doing:
// DAL
// .edmx file generated entities
public IQueryable<TableEntity> GetTableEntities()
{
// read from entity framework and return
}
// BLL
public class TableEntityDTO
{
public int ID { get; set; }
public string Name { get; set; }
// continue on for each column in the table
// and make a DTO class for each table in your database
}
public IEnumerable<TableEntityDTO> ReadTableEntitiesForUser(int userID);
{
var d = new DAL();
var entities = d.GetTableEntities();
// restrict to entites this user "owns"
entities = entities.Where(e => e.OwnerID.Equals(userID));
// convert from "Entity Framework Object" to "BLL Object"
foreach(var e in entities)
{
yeild return new TableEntityDTO() { ID = e.ID, Name = e.Name };
}
}
// WebApp
var b = new BLL();
var myEntities = b.ReadTableEntitiesForUser(1234);
This is true for the Entity Framework that shipped with .NET 3.5SP1 and for Linq-To-SQL both of which I have used a bit, it may hold true for the latest versons of EF, but with Code-First and other things there may be a way to avoid this extra Data-Transfer-Object step, though with a Service Orientated Architecture, DTOs are likely the best way to go.
Originally posted on Stack Overflow — 19 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, May 3, 2011 by Nate Bross
Someone asked on Stack Overflow:
Is it possible to return plain old xml using an asmx file? Without the SOAP stuff?
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
I would recommend you look into a generic handler if your goal is to avoid SOAP. Start here to for some info on using generic handlers, also called ASHX handlers.
From a generic handler, you can return whatever data you like. Using an asmx file for such purposes would be confusing for future developers and it adds complexity that you likely don’t need.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Monday, May 2, 2011 by Nate Bross
Someone asked on Stack Overflow:
I am new to WCF and I’m also learning the MVP design pattern. I have a test project with a working WCF service. I am able to test with the WCF test client and it works fine.
I need help with how to call the WCF service from my Presenter layer and then have the Presenter pass the data back to the View (winforms). I have a Windows Form with two textboxes named txtProductID and txtDescription. I also have a button named btnGetProductData. I would like the following to happen:
- I will put a product id in the txtProductID field.
- I will click the btnGetProductData button and the presenter should call the GetProductData method from the WCF service and return the product description to the txtProductDescription field on my form.
Here is the pertinent code from the WCF service library:
IProductService.cs
------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyWCFServices.ProductService
{
[ServiceContract]
public interface IProductService
{
[OperationContract]
Product GetProductData(string ProductId);
}
[DataContract]
public class Product
{
[DataMember]
public string ProductID { get; set; }
[DataMember]
public string ProductDescription { get; set; }
}
}
ProductService.cs
--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MyWCFServices.ProductEntities;
using MyWCFServices.ProductBusinessLogic;
namespace MyWCFServices.ProductService
{
public class ProductService : IProductService
{
ProductLogic productLogic = new ProductLogic();
public Product GetProductData(string ProductId)
{
ProductEntity productEntity = productLogic.
GetProductData(ProductId);
Product product = new Product();
TranslateProductEntityToProductContractData(productEntity,
product);
return product;
}
private Product TranslateProductEntityToProductContractData(
ProductEntity productEntity, Product product)
{
product.ProductID = productEntity.ProductID;
product.ProductDescription = productEntity.ProductDescription;
return product;
}
}
}
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
I’m not sure where you’re having issues, so I’ll start at the beginning.
- You need to add a “Service Reference” to your Presentation Tier Project (this generates a proxy class you can use to call your service)
- You need to create an instance of the generated proxy class
- You need to call a method on the proxy class and store its value
From Visual Studio, right click your project and choose “Add Service Reference” and then navigate to the endpoint for your service.
Example Code:
// Presentation Tier (button event handler)
var proxy = new ServiceReference1.ProductServiceClient();
var prod = proxy.GetProductData("yourProductID");
txtDescription.Text = prod.Description;
txtProductID.Text = prod.ProductID; // same as passed parameter
Notable comments
Nate (0 upvotes): Yes, you still need to add a service reference in order to use the proxy class I have defined above and that you are trying to use in the code you just posted.
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Sunday, April 24, 2011 by Nate Bross
Someone asked on Stack Overflow:
How can I enable the encryption of an WCF (Windows Communication Foundation) Service with VB.NET?
Is it enough to add a certificate to a service an set to “TransportWithMessageCredential”? Or is there any other settings needed?
If this is enough:
What’s with the client? Maybe I’m wrong but I thought the client and the server need a certificate?!
The client can encrypt messages with the server public key. But how does the server encrypt messages to the client? Or does the client generate an symmetric encryptionkey and send it to the server?
I posted the following answer, which was chosen as the accepted answer:
It depends what type of end point you are using. If you are using an HTTP/HTTPS endpoint, simply having a server certificate is sufficient.
Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.
Friday, April 22, 2011 by Nate Bross
Someone asked on Stack Overflow:
How to enumerate running processes? What about app domains?
Would there be any security-related gotchas?
I would be comfortable with .NET 4.0 only, Windows 7/Windows Server 2008 R2-only solution.
P.S.: This is what I am trying to do… ProcessExplorer.NET question
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
System.Diagnostics.Process.GetProcesses() will list all the running processes as Process objects.
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Friday, April 22, 2011 by Nate Bross
Someone asked on Stack Overflow:
I am about to develop a distributed system using WCF. I need to do the following:
- Send and receive packets ensuring delivery.
- Send and receive echo messages.
- Determine distances (if possible).
- Encrypt data and send them, decript received data.
I need to do this without discovery services or so on. I just need something that allows me to put an IP address and a port and to enstablish a communication.
Is it possible to use TCP? What about UDP?
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
UDP is not a good choice for persistant connections; TCP is a much better choice. What you’re talking about should be possible with WCF; the main issue is going to be getting the first IP address to connect to without having a centralized location.
Basically, you’ll write a WCF service that has a specific endpoint, your service could be hosted by the “client” application so it that you can connect to others running the same program; you’d simply need their IP and port to connect to their end-point.
All that said, depending on the nature of your P2P system, writing your own TCP client and server may serve you better, since WCF is mostly about passing messages back and forth.
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.