Tuesday, August 24, 2010 by Nate Bross
Someone asked on Stack Overflow:
Can anybody tell me why is this not working. I have created a WCF service which returns a list of customers from Northwind database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
namespace WCFSilverlight.Web
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Customers" in code, svc and config file together.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Customers : ICustomers
{
IEnumerable<Customer> ICustomers.GetAllCustomers()
{
NorthwindEntities objNorthwindEntities = new NorthwindEntities();
var query = from cust in objNorthwindEntities.Customers
select cust;
return query.ToList();
}
}
}
And this is my App.xaml.cs code fragment :-
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
CustomersClient objCustomersClient = new CustomersClient();
objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted);
objCustomersClient.GetAllCustomersAsync();
}
void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
If I am not wrong the methods in Silverlight are called asynchronously. So I have added a event handler to handle it and then called the method to retrieve customers. But I don’t get anything in Messagebox. Further when I try to keep a breakpoint on client_GetNameCompleted, it never executes. But if I keep it in Application_Startup it does execute. What can be the problem?
Also explain me am I doing it correct? I’ve seen one example where one person directly defines the function using some strange symbols like =>.
EDIT 1:- Kindly also explain me what is e.UserState in e. What does it contain and what can I possibly do with it?
EDIT 2 :- :- I get this error http://img178.imageshack.us/img178/9070/53923202.jpg
The WCF service is working perfectly i have tested the link query. So there is no problem with Sql Server connection or WCF. Something is wrong with my client only.
This is my ServiceReference.ClientConfig :-
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICustomers" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:50622/Customers.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICustomers" contract="CustomerServ.ICustomers"
name="BasicHttpBinding_ICustomers" />
</client>
</system.serviceModel>
</configuration>
Can you now tell me what is wrong?
Thanks in advance :)
Update :- I read in google you need to set serialization mode to unidirectional. But where do i set this? What do i write where?
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
- You are correct, All network calls in Silverlight are done asynchronously.
- The
=> syntax you mention is shorthand for defining a delegate method, its called a lambda. (see below)
- You should be able to set a break-point in the Completed event handler, if not try restarting Visual Studio (I’ve seen it act strangly before).
e.UserState will have a reference to whatever object you put in the UserState variable for the async call (note the extra overload).
Code:
objCustomersClient.GetAllCustomersCompleted = delegate(object Sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
};
// is the same as
objCustomersClient.GetAllCustomersCompleted += new EventHandler<GetAllCustomersCompletedEventArgs>(client_GetNameCompleted);
void client_GetNameCompleted(object sender, GetAllCustomersCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
// which is same as
objCustomersClient.GetAllCustomersCompleted += (sender, e) => { MessageBox.Show(e.Result.ToString()); };
Notable comments
Nate (0 upvotes): If you are using Linq-To-SQL you’ll need to set Seralization mode on the entities via the Linq-To-SQL designer. Select the table and view its properties. Serialization will be one of them.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, August 24, 2010 by Nate Bross
Someone asked on Stack Overflow:
Using mysql, c#.net, Java Script
I have the web application, i want to create a user limit for my application which means i have to use some license for the users…
For Example
customer need the application for 3 users, so application will work for 3 users, if customer need to extend the user from 3 to 10 means, i have to provide a license for 10 users…
How to create a license for the application, i have to use RSA algorithm for generating a key?
Can any one provide some idea or sample code
Need Help
I posted the following answer, which was chosen as the accepted answer and received 5 upvotes:
There is a large difference between a license key and a license agreement. A license key is something used to make it harder to copy software, since you are deploying a web-app, this shouldn’t be much of an issue for you. A license agreement is a legal document that users of your application must agree to. I’m guessing you are interested in the first.
In a web application, you should implement a login system, and only provide username/passwords for users who have paid you. If as Kobi mentioned, you have a public API that you’d like to license to other users, I recommend you simply give them a uinque GUID (System.Guid.NewGuid();) and then require that GUID as a parameter for every API call. Then, you can check to see if the GUID is associated with a paid account, if so, let it fly, if not abort the transaction with an error code. I would recommend using an HTTPS connection for this.
If, you are trying to sell your web-application to users, so they can host it on their own server, a license key would be a good idea, you might want to add it as a parameter in <appSettings> under web.config. There are many methods for generating license keys, but in at their most simple they involve generating random numbers until a specific check-sum is met. For example, 1253-38 (1+2 = 3, 5+3 = 8). That is a very simple key that would be easy to crack, but you could come up with a more indepth check-sum if you needed.
Originally posted on Stack Overflow — 5 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, August 17, 2010 by Nate Bross
Someone asked on Stack Overflow:
I want to seach more than 15000 values in a select statement as shown below:
select * from tableA where id in (1,2,3......16000)
Can I use threads, say around 3, and partion 15000 values in diffrent select statement.
select * from tableA where id in (1,2,3......5000)
select * from tableA where id in (5001....10000)
select * from tableA where id in (10001....15000)
and run these 3 select statment in parallel.
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
Yes, but the real question is why?
Something like this might get you started:
var itms = new List<YourDataClass>();
var thr1 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
var thr2 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
var thr3 = new Thread(new ThreadStart(delegate()
{
// select code
// populate itms
}));
thr1.Start();
thr2.Start();
thr3.Start();
However, that said, if your IDs are integers and (based on your sample) the range of IN values are sequential, you might want to switch to a where id > 1 and id < 16000 style. This may yeild better performance.
Notable comments
Nate (0 upvotes): Understood, the sample data in your question made it seem like they might be.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Monday, August 2, 2010 by Nate Bross
Someone asked on Server Fault:
can we install IIS to Windows Mobile 6.1 ? As I Have develop a web application in >net and I want to use that same application in Windows Mobile 6.1 ,PDA, as offline application. Ya Surely I will implement that one with Sql server CE but to run it I have to host it on PDA. So can we host it there???
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
Short answer, no, IIS wont run on Windows Mobile.
Long answer (read: alternate solution), your best developing a WinForms (mobile) version using SQL CE.
If you write your code well, place all your business logic within the same assembly, then you can build a regular web interface (asp.net and/or asp.net-mvc) which will implement your business logic, the write the aformentioned WinForms Mobile version, implementing the same class library for business logic.
Notable comments
Nate (0 upvotes): Yes; however, if your PDAs have internet access, they could access the same web interface as everyone else.
Originally posted on Server Fault — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Friday, July 30, 2010 by Nate Bross
Someone asked on Server Fault:
I was hoping to know if there is a way to direct all traffic to a specific url, to the server it corresponds to on our local network with out having to go out into the internet. It would be way more convenient for the system we have set up here.
Please and thank you.
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
The way I’ve done this in the past is by running an internal DNS server that has similar DNS “lookup” zones as the internet, but it simply has local (non-routable) IPs listed.
Then via DHCP, you assign all users to use this DNS server, which will always give out LAN IP addresses for servers you specify.
Originally posted on Server Fault — 2 upvotes (accepted answer). Licensed under CC BY-SA.
Friday, July 30, 2010 by Nate Bross
I asked this on Game Development:
I have been dabling in game development since I started programming, but never very seriously. I work as a business app developer, but I’m working on some games in my spare time.
In the business world (on the Microsft web-dev stack) ASP.NET MVC is becoming really popular, because of its ease of unit-testing the way the interface works.
I’m wondering what design patterns (MVC, MVP, MVVM, etc) one could use to write a game in which all of the game logic is easily unit-testable. Is this possible or feasible? Am I wasting my time, is it better to do full builds and then run “integration” type tests instead of unit-tests?
Sample code would be great, but a writeup is also useful.
(I tried to add a unit-testing tag, but don’t have the rep required…)
Andrew Russell answered (17 upvotes):
Here is a good article that I found that describes an architecture for separating out functionality to make it not only easily reusable, but also potentially far easier to unit-test:
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
Some games will benefit from a MVC-like pattern. Board games like chess and card games come to mind. In most cases, however, this is massive overkill.
Personally I find it is sufficient to only unit-test things that are algorithmic in nature. Things that you will depend on to “just work” when you’re writing gameplay code, and can be insidiously hard to track down problems in, if they don’t. Things like intersection tests or networking code.
(Things that ideally will be built into a 3rd party framework so you don’t have to write or test them!)
One technique I do like to use for unit testing game-related things is what I call the “visual unit test”. The basic concept being a simple line-rendering of the bit of code in question (say for example an intersection function), and some basic key or mouse assignments to manipulate the inputs. No one said unit tests have to be automated - they just have to break things down into individual units and test them.
Originally posted on Game Development — 13 upvotes. Licensed under CC BY-SA.
Tuesday, July 27, 2010 by Nate Bross
Someone asked on Stack Overflow:
I’m looking for some guidance on the overall architecture of this little system I’m building.
Currently, I have an app that is deployed (and updated) via xcopy to a few servers. This works well for updating the code, but it does not work well for updating the period of the code’s execution, since it is setup as a windows scheduled task to run every hour. The xcopy replace works well, because I can run an update from my local machine, and push the new exe file to all the servers. And the next hour, the task scheduler will run the new exe.
I’d like to change it to work this way.
My app runs as a Windows Service and uses System.Threading.Timer to wait a given duration, and then execute the code. This way, part of the process could check the database and if necessary reduce the period of execution for a given server.
The issue there, is that since the .exe would be running as a service, it would not allow for easy updaing via xcopy since the file will be in use all the time.
Any thoughts on how I should set this up to be able to do easy xcopy updates, while running as a windows service? Is that even possible?
I ended up using a bit of @Decker97’s solution, coupled with some powershell magic :)
function global:new-process()
{
param ([string]$computer, [string]$commandline=$(throw "Command line required."))
$path = "\\$computer\root\cimv2:Win32_Process"
$mc = new-object System.Management.ManagementClass $path
$cmdargs = $commandline,$null,$null,0
$ret = $mc.InvokeMethod("Create", $cmdargs)
if ($ret -eq 0) {
write-host "Created Process ID: $($cmdargs[3])"
}
else {
write-host "Error $ret creating process."
}
$mc.Dispose()
}
$serverNames = Get-Content C:\temp\servers.txt
foreach ($server in $serverNames)
{
write-host Updating $server
c:\windows\system32\sc.exe \\$server STOP Service
$source = "C:\Projects\Service\bin\Debug"
$dest = "\\" + $server + "\C$\Program Files\TargetFolder\TargetFolder"
ROBOCOPY.exe $source $dest /MIR /W:5 /R:1
new-process $server 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Installutil.exe /i /LogFile=install.out "C:\Program Files\TargetFolder\TargetFolder\serviceExecutable.exe"'
c:\windows\system32\sc.exe \\$server START Service
}
The script uses service control to try to stop the service.
Then it copies the files to the server with robocopy.
Then use WMI and Powershell to invoke “installutil” remotly on the server.
Then use service control to start up the service.
Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.
Tuesday, July 20, 2010 by Nate Bross
Someone asked on Stack Overflow:
I know all about threading applications, but haven’t done this over web services before. Rather than have the client manage various threads, I want to just fire an async method and get the result from a callback.
It looks straight forward enough - just call the async method in the proxy class generated by visual studio. BUT WAIT… there isn’t anywhere for me to specify a call back.
MSDN and other sites tell me I should be using Begin[MethodName] and End[MethodName], but they don’t appear to exist/been-generated, I do have the [MethodName]Async method which is good (see here), but it doesn’t have an option to pass in a delegate/callback.
I also have (staticly) ServiceName.MethodNameEventHandler/Args available, but can’t use it as it isn’t part of the instantiated service.
What is going on? and how can I send/receive a call back from a web service. I have noticed all the googling I’ve done provides me with pretty old sites. Can anyone provide me with an example.
If you have [MethodName]Async methods, you should have [MethodName]Completed events, which you can subscribe to, and that will act as your callback.
In vb.net it should be something like this:
AddHandler yourWebProxyClassInstance.[MethodName]Completed, AddressOf YourMethod
Notable comments
Nate (0 upvotes): yes, similar. You’ll need to adjust the method signature to match that of the Completed event.
Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.
Thursday, July 15, 2010 by Nate Bross
Someone asked on Stack Overflow:
Given that I have a zip file called archive.zip that contains a file called customerData, how can I programmatically check the date of the file inside archive.zip? I’m using the command-line Winzip utility wzunzip, but I wouldn’t object to possibly using something else.
I’m writing a .net application that will periodically read data from customerData. The file is very big and I want to abort the operation without extracting customerData if the date stamp has not been updated, indicating that there is new data to read.
I posted the following answer, which was chosen as the accepted answer and received 1 upvote:
Via http://dotnetzip.codeplex.com/. There is no native way (that I know) to do that.
Example Code:
ZipFile z = ZipFile.Read(@"C:\archive.zip");
foreach (ZipEntry zEntry in z)
{
Console.WriteLine(zEntry.LastModified.ToString());
}
Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.
Wednesday, July 14, 2010 by Nate Bross
Someone asked on Stack Overflow:
I am working on an asp.net application (.net 4 framework) design and was wanting to know what are the pros and cons and best practices for using webservices vs WCF techology? This application will eventually be used by outside clients to consume data.
When would you use WebServices and when would you use WCF? Is one more scalable than the other?
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
I would use WCF because it can do everything webservices (asmx) does; while giving you the flexibility to extend much further.
You can setup a simple WCF Service just as easily as an ASMX service through Visual Studio. So if you’re “Fresh” on both technologies, I’d spend time learning WCF.
Depending on your specific use-case, you might might also look into WCF Data Services (.NET4) and Entity Framework. It basically gives you a nice API that you can use to consume your database over http/https. The beauty of WCF Data Services, is that you end up writing very little code to get at your data, and you can focus on consuming it.
WCF Getting Started — http://msdn.microsoft.com/en-us/library/ms734712.aspx
WCF Data Services — http://msdn.microsoft.com/en-us/data/ee720180.aspx
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.